File tree Expand file tree Collapse file tree 3 files changed +10
-9
lines changed
ProblemSet/003.Longest Substring Without Repeating Characters Expand file tree Collapse file tree 3 files changed +10
-9
lines changed Original file line number Diff line number Diff line change 11# 无重复字符的最长子串
22
33## 链接
4-
54https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/description/
65
76## 描述
8-
9-
107给定一个字符串,找出不含有重复字符的最长子串的长度。
118
129示例 1:
@@ -30,4 +27,6 @@ https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
3027 请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串。
3128```
3229
30+ ## 思路
31+ 使用一个变量 let map = {} ,记录字符是否出现过
3332
Original file line number Diff line number Diff line change 33 * @return {number }
44 */
55var lengthOfLongestSubstring = function ( s ) {
6- let map = new Map ( ) ;
6+ let map = { } ;
77
88 let max = 0 , tmpLen = 0 , ignore = 0 ;
99 for ( let i = 0 , len = s . length ; i < len ; i ++ ) {
10- if ( ! map . has ( s [ i ] ) || map . get ( s [ i ] ) < ignore ) {
11- map . set ( s [ i ] , i ) ;
10+ if ( map [ s [ i ] ] === undefined || map [ s [ i ] ] < ignore ) {
11+ map [ s [ i ] ] = i ;
1212 tmpLen ++ ;
1313 max = Math . max ( max , tmpLen ) ;
1414 } else {
15- tmpLen = i - map . get ( s [ i ] ) ;
16- ignore = map . get ( s [ i ] ) ; // 这个前面的忽略
17- map . set ( s [ i ] , i ) ;
15+ tmpLen = i - map [ s [ i ] ] ;
16+ ignore = map [ s [ i ] ] ; // 这个前面的忽略
17+ map [ s [ i ] ] = i ;
1818 }
1919 }
2020 return max ;
2121} ;
22+
2223console . log ( lengthOfLongestSubstring ( 'abcabcbb' ) ) ;
2324console . log ( lengthOfLongestSubstring ( 'bbbb' ) ) ;
2425console . log ( lengthOfLongestSubstring ( 'pwwkew' ) ) ;
Original file line number Diff line number Diff line change @@ -23,3 +23,4 @@ _**刷题真心累,真心花时间,主要还是智商不够**_
2323| :---:| :---:| :---:| :---:| :---:|
2424| 001 | [ 两数之和] ( ./ProblemSet/001.Two%20Sum ) | 70 ms | 95% | 简单 |
2525| 002 | [ 两数相加] ( ./ProblemSet/002.Add%20Two%20Numbers ) | 180 ms | 65 % | 中等 |
26+ | 003 | [ 无重复字符的最长子串] ( ./ProblemSet/003.Longest%20Substring%20Without%20Repeating%20Characters ) | 130 ms | 75 % | 中等 |
You can’t perform that action at this time.
0 commit comments