Skip to content

Commit 72226ca

Browse files
Improvement
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 1467860 commit 72226ca

File tree

3 files changed

+28
-38
lines changed

3 files changed

+28
-38
lines changed

0003_longest_substring_without_repeat/longest_substring_without_repeat.c

+8-20
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,19 @@
55

66
int lengthOfLongestSubstring(char *s)
77
{
8-
int offset[128];
9-
int max_len = 0;
8+
int count[256] = {0};
109
int len = 0;
11-
int index = 0;
10+
int i, j;
1211

13-
memset(offset, 0xff, sizeof(offset));
14-
while (*s != '\0') {
15-
if (offset[*s] == -1) {
16-
len++;
17-
} else {
18-
if (index - offset[*s] > len) {
19-
/* not included in sliding window, go on increasing */
20-
len++;
21-
} else {
22-
/* repetition in sliding window, count from scratch */
23-
len = index - offset[*s];
24-
}
12+
for (i = 0, j = 0; s[i] != '\0'; i++) {
13+
count[s[i]]++;
14+
while (count[s[i]] > 1) {
15+
len = i - j > len ? i - j : len;
16+
count[s[j++]] -= 1;
2517
}
26-
if (len > max_len) {
27-
max_len = len;
28-
}
29-
offset[*s++] = index++;
3018
}
3119

32-
return max_len;
20+
return i - j > len ? i - j : len;
3321
}
3422

3523
int main(int argc, char **argv)

0003_longest_substring_without_repeat/longest_substring_without_repeat.cc

+8-16
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@ using namespace std;
55
class Solution {
66
public:
77
int lengthOfLongestSubstring(string s) {
8-
vector<int> indexes(128, -1);
9-
int max_len = 0;
8+
vector<int> count(256);
109
int len = 0;
10+
int i, j;
1111

12-
for (int i = 0; i < s.length(); i++) {
13-
if (indexes[s[i]] == -1) {
14-
len++;
15-
} else {
16-
if (i - indexes[s[i]] > len) {
17-
/* not included in sliding window, go on increasing */
18-
len++;
19-
} else {
20-
/* repetition in sliding window, count from scratch */
21-
len = i - indexes[s[i]];
22-
}
12+
for (i = 0, j = 0; i < s.length(); i++) {
13+
count[s[i]]++;
14+
while (count[s[i]] > 1) {
15+
len = i - j > len ? i - j : len;
16+
count[s[j++]] -= 1;
2317
}
24-
max_len = max(max_len, len);
25-
indexes[s[i]] = i;
2618
}
2719

28-
return max_len;
20+
return i - j > len ? i - j : len;
2921
}
3022
};

0076_minimum_window_substring/window_substring.c

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
6+
/* sliding window pattern
7+
* while (r < size) {
8+
* // check target condition
9+
* while (target_condition) {
10+
* // calculate minimum length
11+
* // iterate left indicator
12+
* }
13+
* // iterate right indicator
14+
* }
15+
*/
516
static char *minWindow(char *s, char *t)
617
{
718
int i, j, count[256] = { 0 };
@@ -42,8 +53,7 @@ static char *minWindow(char *s, char *t)
4253
memcpy(result, s + start, min_len);
4354
result[min_len] = '\0';
4455
} else {
45-
result = malloc(1);
46-
result[0] = '\0';
56+
result = "";
4757
}
4858

4959
return result;

0 commit comments

Comments
 (0)