美文网首页
字符串中不含有重复字符的最长子串

字符串中不含有重复字符的最长子串

作者: RQrry | 来源:发表于2020-03-31 23:07 被阅读0次

给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。

Input: 'abcabcbb'
Output: 3

Input: 'au'
Output: 2

Input: ''
Output: 0

Input: ' '
Output: 1

思路:双指针

var lengthOfLongestSubstring = function(s) {
  let res = 0;
  let len = s.length;
  let i = 0;
  for (let j=i; j<len; j++) {
    let str = s.substring(i, j);
    let index = str.indexOf(s[j]);
    if (index >= 0) {
      res = Math.max(res, str.length);
      i += index + 1;
    } else if (j === len-1) {
      res = Math.max(res, len-i);
    }
  }
  return res;
};

相关文章

网友评论

      本文标题:字符串中不含有重复字符的最长子串

      本文链接:https://www.haomeiwen.com/subject/gzjnuhtx.html