题目如下:

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

这基本就是水题,求最长不重复子串长度。关键在于考察处理边界情况的能力,主要是碰到相同字母,起始位置重新计算这个细节需要注意

代码如下:

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function(s) {
    if(!s) return 0;
    var n = 0;
    var l = s.length;
    var _s = "";
    var _t = 0;
    for (var i = 0;i < l;i++) {
        var flag = _s.indexOf(s[i]);
        if(flag != -1){
            _s = _s.substr(++flag, _s.length) + s[i];
            _t = _s.length;
        } else {
            _s += s[i];
            _t++;
        }
        n = _t > n ? _t : n;
    }
    return n;
};