-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.无重复字符的最长子串.cpp
43 lines (42 loc) · 1.07 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* @lc app=leetcode.cn id=3 lang=cpp
*
* [3] 无重复字符的最长子串
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int start = 0;
int length = s.size();
if(length==0) return 0;
int max_length = 1;
int map_char[128];
// for(int i=0;i<128;i++)
// {
// map_char[i] = -1;
// }
fill_n(map_char,128,-1);
map_char[s[0]] = 0;
for(int j=1;j<length;j++)
{
if(map_char[s[j]]==-1)
{
map_char[s[j]] = j;
if(j-start+1>max_length)
max_length = j-start+1;
}else if(map_char[s[j]]<start)
{
map_char[s[j]] = j;
if(j-start+1>max_length)
max_length = j-start+1;
}else
{
if(j-start>max_length)
max_length = j-start;
start = map_char[s[j]]+1;
map_char[s[j]] = j;
}
}
return max_length;
}
};