Skip to content

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;
};

总结

  1. 大败而归,我觉得面试遇到还是写一个一个的那种吧
  2. while 和for loop卡到那个点的问题,其实我从19年就没弄明白
  3. 可以看下直接跳蛙的那个方法,但还是按这个写吧,别写迷糊了