-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
无重复字符的最长子串 #357
Comments
function lengthOfLongestSubstring(s: string): number {
let i=0,j=0,max=0;
let set:Set<string>=new Set();
while(j<s.length){
if(!set.has(s[j])){
set.add(s[j]);
if(set.size>max)max=set.size;
++j;
}else{
while(set.has(s[j])){
set.delete(s[i++]);
}
set.add(s[j]);
++j;
}
}
return max;
}; |
var lengthOfLongestSubstring = function(s) { let map = new Map(); let max = 0; for(let i=0,j=0; j<s.length ; j++) { if(map.has(s[j])){ i = Math.max(map.get(s[j])+1,i) } max = Math.max(max,j-i+1)//关键,j-i+1 map.set(s[j],j) } return max; }; |
滑动窗口
var lengthOfLongestSubstring = function (s) {
let map = new Set()
let left = 0
let ans = 0
for (let right = 0; right < s.length; right++) {
while (map.has(s[right])) {
map.delete(s[left]) //注意,这里是删除s[left]
left++
}
map.add(s[right])
ans = Math.max(ans, right - left + 1)
}
return ans
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: