码迷,mamicode.com
首页 > 编程语言 > 详细

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

时间:2015-08-26 19:56:50      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

 


 

 

没有重复字母的最长公共子串。

哈希表加双指针,借助哈希表,动态地维护子串。

当一个数不在哈希表中,只需要移动尾指针,并加入表中。

如果已经存在了,循环移动头指针直到找到这个数,把碰到的元素都从哈希表中移除。

 1 /**
 2  * @param {string} s
 3  * @return {number}
 4  */
 5 var lengthOfLongestSubstring = function(s) {
 6     var table = {}, i = 0, j = 0, count = 0, max = 0;
 7     while(j < s.length){
 8         if(!table[s[j]]){
 9             table[s[j]] = true;
10             j++; count++;
11             if(count > max){
12                 max =count;
13             }
14         }else{
15             while(s[i] !== s[j]){
16                 table[s[i]] = false;
17                 i++; count--;
18             }
19             i++; j++;
20         }
21     }
22     return max;
23 };

 

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/Liok3187/p/4761320.html

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