solution:
可以使用double pointers. 需要更新最左边和最右边。题目不难, 这时就是看代码怎么写的简洁。
代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = 0;
int left = 0;
vector<int> m(256, 0);
for (int i=0; i<s.size(); ++i){
left = max(left, m[s[i]]);
m[s[i]] = i+1;
res = max(res, i-left+1);
}
return res;
}
};
No comments :
Post a Comment