https://leetcode.com/problems/longest-substring-without-repeating-characters
My answer / AC
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
if(s=="") return 0;
if(s.length==1) return 1;
let left = 0, right = 0, ans=0;
let map = new Set();
while(right<s.length && left<s.length) {
if(!map.has(s[right])) {
map.add(s[right]);
right++;
ans = Math.max(ans, right-left);
} else {
map.delete(s[left]);
left++;
}
}
console.log(map)
return ans;
};
左指针用来表示当前处理字符串的头,右指针用来代表尾。
详见https://leetcode.wang/leetCode-3-Longest-Substring-Without-Repeating-Characters.html的解法1
Best answer
网友评论