美文网首页
[JavaScript LeetCode]3. Longest

[JavaScript LeetCode]3. Longest

作者: Instincts | 来源:发表于2017-06-06 10:14 被阅读173次

    原题链接
    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);
    };
    

    相关文章

      网友评论

          本文标题:[JavaScript LeetCode]3. Longest

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