Skip to content

Commit 03d9dea

Browse files
committed
feat: update solutions to lc problem: No.0003
No.0003.Longest Substring Without Repeating Characters
1 parent 0daeb2c commit 03d9dea

File tree

6 files changed

+138
-83
lines changed

6 files changed

+138
-83
lines changed

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@
5858

5959
“滑动窗口 + 哈希表”。
6060

61-
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的结束位置和开始位置,res 表示无重复字符子串的最大长度。
61+
定义一个哈希表记录当前窗口内出现的字符,i、j 分别表示不重复子串的开始位置和结束位置,ans 表示无重复字符子串的最大长度。
6262

63-
遍历 i,若 `[j, i - 1]` 窗口内存在 `s[i]`,则 j 循环向右移动,更新哈希表,直至 `[j, i - 1]` 窗口不存在 `s[i]`,循环结束。将 `s[i]` 加入哈希表中,此时 `[j, i]` 窗口内不含重复元素,更新 res 的最大值:`res = max(res, i - j + 1)`
63+
遍历 s 每个字符 c,若 `[i, j - 1]` 窗口内存在 `c`,则 i 循环向右移动,更新哈希表,直至 `[i, j - 1]` 窗口不存在 `c`,循环结束。将 `c` 加入哈希表中,此时 `[i, j]` 窗口内不含重复元素,更新 ans 的最大值:`res = max(ans, j - i + 1)`
6464

65-
最后返回 res 即可。
65+
最后返回 ans 即可。
6666

6767
<!-- tabs:start -->
6868

@@ -73,17 +73,15 @@
7373
```python
7474
class Solution:
7575
def lengthOfLongestSubstring(self, s: str) -> int:
76-
i = j = res = 0
76+
i = ans = 0
7777
chars = set()
78-
while i < len(s):
79-
while s[i] in chars:
80-
if s[j] in chars:
81-
chars.remove(s[j])
82-
j += 1
83-
chars.add(s[i])
84-
res = max(res, i - j + 1)
85-
i += 1
86-
return res
78+
for j, c in enumerate(s):
79+
while c in chars:
80+
chars.remove(s[i])
81+
i += 1
82+
chars.add(c)
83+
ans = max(ans, j - i + 1)
84+
return ans
8785
```
8886

8987
### **Java**
@@ -93,17 +91,17 @@ class Solution:
9391
```java
9492
class Solution {
9593
public int lengthOfLongestSubstring(String s) {
96-
int res = 0;
97-
Set<Character> set = new HashSet<>();
98-
for (int i = 0, j = 0; i < s.length(); ++i) {
99-
char c = s.charAt(i);
100-
while (set.contains(c)) {
101-
set.remove(s.charAt(j++));
94+
int i = 0, j = 0, ans = 0;
95+
Set<Character> chars = new HashSet<>();
96+
for (char c : s.toCharArray()) {
97+
while (chars.contains(c)) {
98+
chars.remove(s.charAt(i++));
10299
}
103-
set.add(c);
104-
res = Math.max(res, i - j + 1);
100+
chars.add(c);
101+
ans = Math.max(ans, j - i + 1);
102+
++j;
105103
}
106-
return res;
104+
return ans;
107105
}
108106
}
109107
```
@@ -115,17 +113,18 @@ class Solution {
115113
* @param {string} s
116114
* @return {number}
117115
*/
118-
var lengthOfLongestSubstring = function(s) {
119-
let res = 0;
116+
var lengthOfLongestSubstring = function(s) {
117+
let i = 0, j = 0, ans = 0;
120118
let chars = new Set();
121-
for (let i = 0, j = 0; i < s.length; ++i) {
122-
while (chars.has(s[i])) {
123-
chars.delete(s[j++]);
119+
for (let c of s) {
120+
while (chars.has(c)) {
121+
chars.delete(s[i++]);
124122
}
125-
chars.add(s[i]);
126-
res = Math.max(res, i - j + 1);
123+
chars.add(c);
124+
ans = Math.max(ans, j - i + 1);
125+
++j;
127126
}
128-
return res;
127+
return ans;
129128
};
130129
```
131130

@@ -202,6 +201,27 @@ func max(x, y int) int {
202201
}
203202
```
204203

204+
### **C++**
205+
206+
```cpp
207+
class Solution {
208+
public:
209+
int lengthOfLongestSubstring(string s) {
210+
int i = 0, j = 0, ans = 0;
211+
unordered_set<char> chars;
212+
for (char& c : s)
213+
{
214+
while (chars.count(c)) chars.erase(s[i++]);
215+
chars.insert(c);
216+
ans = max(ans, j - i + 1);
217+
++j;
218+
}
219+
return ans;
220+
221+
}
222+
};
223+
```
224+
205225
### **Nim**
206226
207227
```nim

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,33 @@ Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence an
5757
```python
5858
class Solution:
5959
def lengthOfLongestSubstring(self, s: str) -> int:
60-
i = j = res = 0
60+
i = ans = 0
6161
chars = set()
62-
while i < len(s):
63-
while s[i] in chars:
64-
if s[j] in chars:
65-
chars.remove(s[j])
66-
j += 1
67-
chars.add(s[i])
68-
res = max(res, i - j + 1)
69-
i += 1
70-
return res
62+
for j, c in enumerate(s):
63+
while c in chars:
64+
chars.remove(s[i])
65+
i += 1
66+
chars.add(c)
67+
ans = max(ans, j - i + 1)
68+
return ans
7169
```
7270

7371
### **Java**
7472

7573
```java
7674
class Solution {
7775
public int lengthOfLongestSubstring(String s) {
78-
int res = 0;
79-
Set<Character> set = new HashSet<>();
80-
for (int i = 0, j = 0; i < s.length(); ++i) {
81-
char c = s.charAt(i);
82-
while (set.contains(c)) {
83-
set.remove(s.charAt(j++));
76+
int i = 0, j = 0, ans = 0;
77+
Set<Character> chars = new HashSet<>();
78+
for (char c : s.toCharArray()) {
79+
while (chars.contains(c)) {
80+
chars.remove(s.charAt(i++));
8481
}
85-
set.add(c);
86-
res = Math.max(res, i - j + 1);
82+
chars.add(c);
83+
ans = Math.max(ans, j - i + 1);
84+
++j;
8785
}
88-
return res;
86+
return ans;
8987
}
9088
}
9189
```
@@ -97,17 +95,18 @@ class Solution {
9795
* @param {string} s
9896
* @return {number}
9997
*/
100-
var lengthOfLongestSubstring = function(s) {
101-
let res = 0;
98+
var lengthOfLongestSubstring = function(s) {
99+
let i = 0, j = 0, ans = 0;
102100
let chars = new Set();
103-
for (let i = 0, j = 0; i < s.length; ++i) {
104-
while (chars.has(s[i])) {
105-
chars.delete(s[j++]);
101+
for (let c of s) {
102+
while (chars.has(c)) {
103+
chars.delete(s[i++]);
106104
}
107-
chars.add(s[i]);
108-
res = Math.max(res, i - j + 1);
105+
chars.add(c);
106+
ans = Math.max(ans, j - i + 1);
107+
++j;
109108
}
110-
return res;
109+
return ans;
111110
};
112111
```
113112

@@ -184,6 +183,27 @@ func max(x, y int) int {
184183
}
185184
```
186185

186+
### **C++**
187+
188+
```cpp
189+
class Solution {
190+
public:
191+
int lengthOfLongestSubstring(string s) {
192+
int i = 0, j = 0, ans = 0;
193+
unordered_set<char> chars;
194+
for (char& c : s)
195+
{
196+
while (chars.count(c)) chars.erase(s[i++]);
197+
chars.insert(c);
198+
ans = max(ans, j - i + 1);
199+
++j;
200+
}
201+
return ans;
202+
203+
}
204+
};
205+
```
206+
187207
### **Nim**
188208
189209
```nim
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int lengthOfLongestSubstring(string s) {
4+
int i = 0, j = 0, ans = 0;
5+
unordered_set<char> chars;
6+
for (char& c : s)
7+
{
8+
while (chars.count(c)) chars.erase(s[i++]);
9+
chars.insert(c);
10+
ans = max(ans, j - i + 1);
11+
++j;
12+
}
13+
return ans;
14+
15+
}
16+
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int lengthOfLongestSubstring(String s) {
3-
int res = 0;
4-
Set<Character> set = new HashSet<>();
5-
for (int i = 0, j = 0; i < s.length(); ++i) {
6-
char c = s.charAt(i);
7-
while (set.contains(c)) {
8-
set.remove(s.charAt(j++));
3+
int i = 0, j = 0, ans = 0;
4+
Set<Character> chars = new HashSet<>();
5+
for (char c : s.toCharArray()) {
6+
while (chars.contains(c)) {
7+
chars.remove(s.charAt(i++));
98
}
10-
set.add(c);
11-
res = Math.max(res, i - j + 1);
9+
chars.add(c);
10+
ans = Math.max(ans, j - i + 1);
11+
++j;
1212
}
13-
return res;
13+
return ans;
1414
}
1515
}

solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* @return {number}
44
*/
55
var lengthOfLongestSubstring = function(s) {
6-
let res = 0;
6+
let i = 0, j = 0, ans = 0;
77
let chars = new Set();
8-
for (let i = 0, j = 0; i < s.length; ++i) {
9-
while (chars.has(s[i])) {
10-
chars.delete(s[j++]);
8+
for (let c of s) {
9+
while (chars.has(c)) {
10+
chars.delete(s[i++]);
1111
}
12-
chars.add(s[i]);
13-
res = Math.max(res, i - j + 1);
12+
chars.add(c);
13+
ans = Math.max(ans, j - i + 1);
14+
++j;
1415
}
15-
return res;
16+
return ans;
1617
};
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class Solution:
22
def lengthOfLongestSubstring(self, s: str) -> int:
3-
i = j = res = 0
3+
i = ans = 0
44
chars = set()
5-
while i < len(s):
6-
while s[i] in chars:
7-
if s[j] in chars:
8-
chars.remove(s[j])
9-
j += 1
10-
chars.add(s[i])
11-
res = max(res, i - j + 1)
12-
i += 1
13-
return res
5+
for j, c in enumerate(s):
6+
while c in chars:
7+
chars.remove(s[i])
8+
i += 1
9+
chars.add(c)
10+
ans = max(ans, j - i + 1)
11+
return ans

0 commit comments

Comments
 (0)