码迷,mamicode.com
首页 > Web开发 > 详细

[JS做LeetCode] Longest Substring Without Repeating Characters

时间:2015-03-21 18:29:41      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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
 

[JS做LeetCode] Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/oadaM92/p/4355851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!