leetcode 无重复字符的最长子串 中等_尺取

 

 

尺取。



class Solution {
public:
int lengthOfLongestSubstring(const string &s) {
// 128 ASCII
bool map[128] = {false};
int ans = 0, l = 0;
for(int r = 0; r < s.size(); ++ r) {
while(l < r && map[s[r]]) {
map[s[l]] = false;
++ l;
}
ans = max(ans, r - l + 1);
map[s[r]] = true;
}
return ans;
}
};