-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp003.cpp
52 lines (52 loc) · 1.26 KB
/
p003.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int len = s.length();
if (len == 0) return 0;
bool used[256] = {0};
int lastend = 0;
int i, j;
used[s[0]] = true;
for (i = 1; i < len; i++)
{
if (!used[s[i]])
{
lastend = i;
used[s[i]] = true;
}
else
{
break;
}
}
int result = lastend+1;
if (i == len) return result;
for (i = 1; i < len; i++)
{
used[s[i-1]] = false;
if (lastend < i)
{
lastend = i;
used[s[i]] = true;
}
for (j = lastend+1; j < len; j++)
{
if (!used[s[j]])
{
lastend = j;
used[s[j]] = true;
}
else
{
break;
}
}
if (lastend-i+1 > result)
{
result = lastend-i+1;
}
if (result >= len-i-1) return result;
}
return result;
}
};