Longest Substring Without Repeat Characters

 1 var lengthOfLongestSubstring = function(s) {
 2     if (s === '') {
 3         return 0;
 4     }
 5 
 6     var lenMax = 1,
 7         lenCurr = 1,
 8         i, repeat;
 9 
10     for (i = 1; i < s.length; i++) {
11         repeat = s.substr(i - lenCurr, lenCurr).indexOf(s.substr(i, 1));
12 
13         if (repeat == -1) {
14             lenCurr++;
15         } else {
16             lenCurr -= repeat;
17         }
18 
19         if (lenCurr > lenMax) {
20             lenMax = lenCurr;
21         }
22     }
23 
24     return lenMax;
25 };

 

posted @ 2015-12-24 20:32  叫我霍啊啊啊  阅读(168)  评论(0编辑  收藏  举报