AM 384. Longest Substring Without Repeating Characters 和 Amazon OA 763. Partition Labels有些相似
- Get link
- X
- Other Apps
class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string &s) {
// write your code here
//思路最重要。之前做出了,现在做写出了bug。
//因为之前做时,把握了整体思路。就是一边遍历时,一边确认左边有效边界。
// 有这个思路做指导 就不会有bug。思路!!
int res = 1;
unordered_map<char, int> mp;
const int size = s.size();
if(size < 2){
return size;
}
int left = -1;
for(int i = 0; i < size; i++){
if(mp.find(s[i]) != mp.end()){
left = max(left, mp[s[i]]);
mp[s[i]] = i;
}
else{
mp[s[i]] = i;
}
res = max(res, i - left);
}
return res;
}
};- 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
Comments
Post a Comment