我的js算法爬坑之旅- 最长不含重复字符的子字符串

341 阅读1分钟

第九十七天:剑指offer 48题,最长不含重复字符的子字符串

地址:leetcode-cn.com/problems/zu…

思路:滑动窗口

var lengthOfLongestSubstring = function(s) {
  s = s.split('');
  const n = s.length;
  let max = 0;
  for(let i = 0; i < n - max; i++)
  {
    let res = [];
    let j = i;
    while(res.indexOf(s[j]) === -1 && j < n)
    {
      res.push(s[j]);
      j++;
    }
    max = Math.max(max, res.length);
  }
  return max;
};

代码挺冗杂的

执行用时:568 ms, 在所有 JavaScript 提交中击败了5.06%的用户

内存消耗:44.1 MB, 在所有 JavaScript 提交中击败了18.13%的用户