-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestSubstringWithoutRepeatingCharacters.cpp
More file actions
60 lines (56 loc) · 1.63 KB
/
LongestSubstringWithoutRepeatingCharacters.cpp
File metadata and controls
60 lines (56 loc) · 1.63 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/***
* 贪心法
* 双指针 + hash,维护一个区间
* 用一个map保存已经遍历的字符和字符出现的位置
* 如果该字符已经出现,则计算当前窗口的长度,并更新字符位置,滑动窗口
* 记录最长的窗口长度
***/
/*
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (0 == s.size())
return 0;
map<char, int> mmap;
int maxw = 0;
int wstart = 0;
for (int wend = 0; wend < s.size(); ++wend)
{
map<char, int>::iterator it = mmap.find(s[wend]);
if ((it != mmap.end()) && (it->second >= wstart)) // repeat char
{
int window = wend - wstart; // update min window
wstart = it->second + 1; // index of pre char + 1
if (window > maxw)
maxw = window;
} // else
mmap[s[wend]] = wend; // if exist, update; else, add a new entry
}
maxw = max(maxw, (int)(s.size() - wstart)); // caution: the last seg!!
return maxw;
}
};
*/
/***
* 法2:如果是ASCII码值,直接开一个长为256的数组
***/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> vmap(256, -1); // index
int maxl = 0;
int wstart = 0;
for (int i = 0; i < s.size(); ++i)
{
if (vmap[s[i]] >= wstart)
{
int window = i - wstart;
wstart = vmap[s[i]] + 1;
maxl = max(maxl, window);
}
vmap[s[i]] = i;
}
maxl = max(maxl, (int)(s.size() - wstart)); // caution: the last seg!!
return maxl;
}
};