首页 > 编程语言 > 详细

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

时间:2015-08-26 19:55:11      阅读:130      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!