-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#3.cc
More file actions
38 lines (32 loc) · 918 Bytes
/
LeetCode#3.cc
File metadata and controls
38 lines (32 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<string.h>
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int cnt[150];
memset(cnt,0,sizeof(cnt));
int ret = 0;
int tail = 0, head = 0;
int len = s.length();
while(head < len){
if(cnt[(int)s[head]]==0){
cnt[(int)s[head]] = 1;
if(ret < head-tail+1)
ret = head - tail + 1;
head++;
}
else{
while(s[tail]!=s[head]) {
cnt[(int)s[tail]] = 0;
tail++;
}
tail++;
if(ret < head-tail+1)
ret = head-tail+1;
head++;
}
}
return ret;
}
};