Skip to content

Commit f26dea9

Browse files
committed
feat: update solutions: 0003. Longest Substring Without Repeating Chars
1 parent ad48bcb commit f26dea9

File tree

6 files changed

+81
-24
lines changed

6 files changed

+81
-24
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767

6868
### 字符串
6969

70+
- [无重复字符的最长子串](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README.md)
7071
- [反转字符串中的元音字母](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README.md)
7172
- [字符串转换整数 (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README.md)
7273
- [赎金信](/solution/0300-0399/0383.Ransom%20Note/README.md)

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6666

6767
### Strings
6868

69+
- [Longest Substring Without Repeating Characters](/solution/0000-0099/0003.Longest%20Substring%20Without%20Repeating%20Characters/README_EN.md)
6970
- [Reverse Vowels of a String](/solution/0300-0399/0345.Reverse%20Vowels%20of%20a%20String/README_EN.md)
7071
- [String to Integer (atoi)](/solution/0000-0099/0008.String%20to%20Integer%20%28atoi%29/README_EN.md)
7172
- [Ransom Note](/solution/0300-0399/0383.Ransom%20Note/README_EN.md)

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

+34-2
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,54 @@
3333

3434
<!-- 这里可写通用的实现逻辑 -->
3535

36+
- 定义一个哈希表存放字符及其出现的位置;
37+
- 定义 i, j 分别表示不重复子串的开始位置和结束位置;
38+
- j 向后遍历,若遇到与 `[i, j]` 区间内字符相同的元素,更新 i 的值,此时 `[i, j]` 区间内不存在重复字符,计算 res 的最大值。
39+
3640
<!-- tabs:start -->
3741

3842
### **Python3**
3943

4044
<!-- 这里可写当前语言的特殊实现逻辑 -->
4145

4246
```python
43-
47+
class Solution:
48+
def lengthOfLongestSubstring(self, s: str) -> int:
49+
res, chars = 0, dict()
50+
i = j = 0
51+
while j < len(s):
52+
if s[j] in chars:
53+
# chars[s[j]]+1 可能比 i 还小,通过 max 函数来锁住左边界
54+
# e.g. 在"tmmzuxt"这个字符串中,遍历到最后一步时,最后一个字符't'和第一个字符't'是相等的。如果没有 max 函数,i 就会回到第一个't'的索引0处的下一个位置
55+
i = max(i, chars[s[j]] + 1)
56+
res = max(res, j - i + 1)
57+
chars[s[j]] = j
58+
j += 1
59+
return res
4460
```
4561

4662
### **Java**
4763

4864
<!-- 这里可写当前语言的特殊实现逻辑 -->
4965

5066
```java
51-
67+
class Solution {
68+
public int lengthOfLongestSubstring(String s) {
69+
int res = 0;
70+
Map<Character, Integer> chars = new HashMap<>();
71+
for (int i = 0, j = 0; j < s.length(); ++j) {
72+
char c = s.charAt(j);
73+
if (chars.containsKey(c)) {
74+
// chars.get(c)+1 可能比 i 还小,通过 max 函数来锁住左边界
75+
// e.g. 在"tmmzuxt"这个字符串中,遍历到最后一步时,最后一个字符't'和第一个字符't'是相等的。如果没有 max 函数,i 就会回到第一个't'的索引0处的下一个位置
76+
i = Math.max(i, chars.get(c) + 1);
77+
}
78+
chars.put(c, j);
79+
res = Math.max(res, j - i + 1);
80+
}
81+
return res;
82+
}
83+
}
5284
```
5385

5486
### **...**

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

+26-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,37 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def lengthOfLongestSubstring(self, s: str) -> int:
68+
res, chars = 0, dict()
69+
i = j = 0
70+
while j < len(s):
71+
if s[j] in chars:
72+
i = max(i, chars[s[j]] + 1)
73+
res = max(res, j - i + 1)
74+
chars[s[j]] = j
75+
j += 1
76+
return res
6777
```
6878

6979
### **Java**
7080

7181
```java
72-
82+
class Solution {
83+
public int lengthOfLongestSubstring(String s) {
84+
int res = 0;
85+
Map<Character, Integer> chars = new HashMap<>();
86+
for (int i = 0, j = 0; j < s.length(); ++j) {
87+
char c = s.charAt(j);
88+
if (chars.containsKey(c)) {
89+
i = Math.max(i, chars.get(c) + 1);
90+
}
91+
chars.put(c, j);
92+
res = Math.max(res, j - i + 1);
93+
}
94+
return res;
95+
}
96+
}
7397
```
7498

7599
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Solution {
22
public int lengthOfLongestSubstring(String s) {
3-
Map<Character, Integer> map = new HashMap<>();
4-
int max = 0;
5-
for (int fast = 0, slow = 0; fast < s.length(); fast ++) {
6-
if (map.containsKey(s.charAt(fast))) {
7-
int target = map.get(s.charAt(fast)) + 1;
8-
slow = target < slow ? slow : target;
3+
int res = 0;
4+
Map<Character, Integer> chars = new HashMap<>();
5+
for (int i = 0, j = 0; j < s.length(); ++j) {
6+
char c = s.charAt(j);
7+
if (chars.containsKey(c)) {
8+
i = Math.max(i, chars.get(c) + 1);
99
}
10-
map.put(s.charAt(fast), fast);
11-
max = Math.max(max, fast - slow + 1);
10+
chars.put(c, j);
11+
res = Math.max(res, j - i + 1);
1212
}
13-
return max;
13+
return res;
1414
}
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
class Solution:
2-
def lengthOfLongestSubstring(self, s: str):
3-
max = 0
4-
length = len(s)
5-
substr = ""
6-
for i in range(0, length):
7-
index = substr.find(s[i])
8-
if index > -1:
9-
substr = substr[index + 1:]
10-
substr += s[i]
11-
max = (max if (max > len(substr)) else len(substr))
12-
return max
2+
def lengthOfLongestSubstring(self, s: str) -> int:
3+
res, chars = 0, dict()
4+
i = j = 0
5+
while j < len(s):
6+
if s[j] in chars:
7+
i = max(i, chars[s[j]] + 1)
8+
res = max(res, j - i + 1)
9+
chars[s[j]] = j
10+
j += 1
11+
return res

0 commit comments

Comments
 (0)