Appearance
003无重复子串
code
javascript
/**
* @param {string} s
* @return {number}
*/
// var lengthOfLongestSubstring = function (s) {
// let left = 0;
// let right = 0;
// let max = 0;
// let used = {};
// while (right < s.length) {
// if (used[s.charAt(right)] !== undefined) {
// delete used[s.charAt(left)];
// left++;
// max = Math.max(max, right - left);
// continue;
// }
// used[s.charAt(right)] = true;
// right++;
// max = Math.max(max, right - left);
// }
// return max;
// };
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
let ind_left = 0;
let ind_right = 0;
let map = {};
let max = 0;
while(ind_right < s.length){
if(map[s.charAt(ind_right)]){
// undefined/ 0 或者啥的falsy都走else
map[s.charAt(ind_left)] = false;
ind_left++;
} else {
map[s.charAt(ind_right)] = true;
max = Math.max(max, ind_right - ind_left + 1);
ind_right++;
}
}
return max;
};总结
- 大败而归,我觉得面试遇到还是写一个一个的那种吧
- while 和for loop卡到那个点的问题,其实我从19年就没弄明白
- 可以看下直接跳蛙的那个方法,但还是按这个写吧,别写迷糊了
2026.8.6 补充
- 笨法能k,非常的慢虽然
- 我知道直接跳上次位置这件事,反正属于需要写好的吧