原题链接
Given a string, find the length of the longest substring without repeating characters.
代码
附上JavaScript版本
var lengthOfLongestSubstring = function(s) {
if (s.length < 2) {
return s.length;
}
var maxLen = 0;
var l = 0;
for (var r = 1; r < s.length; r++) {
i = s.lastIndexOf(s[r], r-1);
if (i >= 0) {
maxLen = Math.max(maxLen, r - l);
l = Math.max(l, i + 1);
}
}
return Math.max(maxLen, r - l);
};