Skip to content

Commit 6a90f22

Browse files
committed
lc 003
1 parent 12b83c3 commit 6a90f22

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

LeetCode/ProblemSet/003.Longest Substring Without Repeating Characters/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# 无重复字符的最长子串
22

33
## 链接
4-
54
https://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

LeetCode/ProblemSet/003.Longest Substring Without Repeating Characters/longest-substring-without-repeating-characters.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
* @return {number}
44
*/
55
var 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+
2223
console.log(lengthOfLongestSubstring('abcabcbb'));
2324
console.log(lengthOfLongestSubstring('bbbb'));
2425
console.log(lengthOfLongestSubstring('pwwkew'));

LeetCode/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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 % | 中等 |

0 commit comments

Comments
 (0)