[JS做LeetCode] Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters

<html>

<head>
    <meta http-equiv="charset" content="utf-8"></head>

<body>
    <script>
        // 解题思路:
        // 设置两个指针,start和end,end一直向后走,如果遇到前面出现过的字符串,start向前走一步
        var s = ‘asdasdas‘;
        var lengthOfLongestSubstring = function(s) {
                var length = 0;//lengthOfLongestSubstring
                var len = s.length;
                var start=0;
                var end=0;
                var tempArr = [];
                while(end !== len) {
                        if(tempArr.indexOf(s[end])===-1) {
                                tempArr.push(s[end]);
                                if(tempArr.length > length) length = tempArr.length;
                                end++;
                        } else {
                                tempArr.shift();
                                start++;
                        }
                }
                return length;
        };

        alert(lengthOfLongestSubstring(s));
    </script></body>

</htm
 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。