leetcode longest substring without repeating characters

文章目录

tips

  1. 求没有重复字符的最长子串
  2. memset(who, value, length)
  3. left记录子串的最左边界,如果当前字符在left右面出现过,最更新left
  4. time complexcity: O(N)
  5. space complexcity: O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class  {
public:
int lengthOfLongestSubstring(string s) {
int left = 0, ans = 0;
int last[255];
memset(last, -1, sizeof(last));
for(int i = 0; i < s.size(); ++i) {
if(last[s[i]] >= left) {
left = last[s[i]] + 1;
}
last[s[i]] = i;
ans = max(ans, i - left + 1);
}
return ans;
}
};