码迷,mamicode.com
首页 > 其他好文 > 详细

3. Longest Substring Without Repeating Characters

时间:2017-10-06 10:36:15      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:char   examples   ==   ati   style   abc   and   indexof   logs   

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 

 

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var lengthOfLongestSubstring = function(s) {
 6 
 7     var len = s.length,
 8         local =1;
 9         max = 1,
10         start = 0;
11 
12     //处理"";
13     if(!len){
14         return 0;
15     }
16     
17     
18     for(var end = 1;end < len;end++){
19         
20         var c = s.charAt(end);
21         var pattern = s.substring(start,end);
22         var index = pattern.indexOf(c);
23         
24         if(index !== -1){
25             
26             local = end - start - index; 
27             start = start + index + 1;
28         }else{
29             local += 1;
30             max = local > max ? local : max;
31         }
32         
33     }
34     return max;
35 };

 

 

3. Longest Substring Without Repeating Characters

标签:char   examples   ==   ati   style   abc   and   indexof   logs   

原文地址:http://www.cnblogs.com/huenchao/p/7630381.html

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