给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
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;
};
网友评论