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 };