Skip to content

Commit c05b7a5

Browse files
committed
solution of 0003 done in c
1 parent 2e3bd2f commit c05b7a5

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,34 @@ class Solution {
300300
}
301301
```
302302

303+
#### C
304+
305+
``` c
306+
int lengthOfLongestSubstring(char *s) {
307+
int freq[256] = {0};
308+
int l = 0, r = 0;
309+
int ans = 0;
310+
int len = strlen(s);
311+
312+
for (r = 0; r < len; r++) {
313+
char c = s[r];
314+
freq[(unsigned char)c]++;
315+
316+
while (freq[(unsigned char)c] > 1) {
317+
freq[(unsigned char)s[l]]--;
318+
l++;
319+
}
320+
321+
if (ans < r - l + 1) {
322+
ans = r - l + 1;
323+
}
324+
}
325+
326+
return ans;
327+
}
328+
329+
```
330+
303331
<!-- tabs:end -->
304332
305333
<!-- solution:end -->

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,35 @@ class Solution {
298298
}
299299
```
300300

301+
#### C
302+
303+
```c
304+
305+
int lengthOfLongestSubstring(char *s) {
306+
int freq[256] = {0};
307+
int l = 0, r = 0;
308+
int ans = 0;
309+
int len = strlen(s);
310+
311+
for (r = 0; r < len; r++) {
312+
char c = s[r];
313+
freq[(unsigned char)c]++;
314+
315+
while (freq[(unsigned char)c] > 1) {
316+
freq[(unsigned char)s[l]]--;
317+
l++;
318+
}
319+
320+
if (ans < r - l + 1) {
321+
ans = r - l + 1;
322+
}
323+
}
324+
325+
return ans;
326+
}
327+
328+
```
329+
301330
<!-- tabs:end -->
302331
303332
<!-- solution:end -->
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
int lengthOfLongestSubstring(char *s) {
2+
int freq[256] = {0};
3+
int l = 0, r = 0;
4+
int ans = 0;
5+
int len = strlen(s);
6+
7+
for (r = 0; r < len; r++) {
8+
char c = s[r];
9+
freq[(unsigned char)c]++;
10+
11+
while (freq[(unsigned char)c] > 1) {
12+
freq[(unsigned char)s[l]]--;
13+
l++;
14+
}
15+
16+
if (ans < r - l + 1) {
17+
ans = r - l + 1;
18+
}
19+
}
20+
21+
return ans;
22+
}

0 commit comments

Comments
 (0)