forked from arkingc/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.cpp
28 lines (23 loc) · 746 Bytes
/
3.cpp
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
const int range = 256;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int state[range];
int globalmax = 0,localmax = 0;
int beginidx = 0;
for(int i = 0;i < range;i++) state[i] = -1;
for(int idx = 0;idx < s.size();idx++){
if(state[s[idx]] < 0 || state[s[idx]] < beginidx){
localmax++;
state[s[idx]] = idx;
if(localmax > globalmax) globalmax = localmax;
}
else{
beginidx = state[s[idx]] + 1;
localmax = idx - beginidx + 1;
state[s[idx]] = idx;
}
}
return globalmax;
}
};