-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.cpp
More file actions
39 lines (36 loc) · 1.06 KB
/
3.cpp
File metadata and controls
39 lines (36 loc) · 1.06 KB
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
// class Solution(object):
// def lengthOfLongestSubstring(self, s):
// """
// :type s: str
// :rtype: int
// """
// a = [[0 for i in range(len(s))] for j in range(len(s))]
// for i in range(len(s)):
// a[i][i] = 1
// for j in range(i + 1, len(s)):
// temp = a[i][i]
// for k in range(i, j):
// if s[j] in s[k : j]:
// temp = max(temp, a[k][j-1])
// else:
// temp = max(temp, a[k][j-1] + 1)
// return a[len(s)-1][len(s)-1]
#include <iostream>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int ret = 0, left = 0;
int a[256] = {0};
for(int i = 0 ; i < s.size(); ++i){
if(a[s[i]] == 0 || a[s[i]] < left ){
ret = max(ret, i - left + 1);
}
else{
left = a[s[i]];
}
a[s[i]] = i + 1;
}
return ret;
}
};