Skip to content

Commit 5b7be54

Browse files
committed
feat: add solutions to lc problem: No.0003
No.0003.Longest Substring Without Repeating Characters
1 parent cb1676f commit 5b7be54

File tree

11 files changed

+241
-236
lines changed

11 files changed

+241
-236
lines changed

solution/0000-0099/0002.Add Two Numbers/Solution.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
7+
def addTwoNumbers(
8+
self, l1: Optional[ListNode], l2: Optional[ListNode]
9+
) -> Optional[ListNode]:
810
dummy = ListNode()
911
carry, curr = 0, dummy
1012
while l1 or l2 or carry:

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

+93-83
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050

5151
**方法一:双指针 + 哈希表**
5252

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

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

5757
最后返回 $ans$ 即可。
5858

@@ -78,13 +78,13 @@ for (int i = 0, j = 0; i < n; ++i) {
7878
```python
7979
class Solution:
8080
def lengthOfLongestSubstring(self, s: str) -> int:
81+
ss = set()
8182
i = ans = 0
82-
chars = set()
8383
for j, c in enumerate(s):
84-
while c in chars:
85-
chars.remove(s[i])
84+
while c in ss:
85+
ss.remove(s[i])
8686
i += 1
87-
chars.add(c)
87+
ss.add(c)
8888
ans = max(ans, j - i + 1)
8989
return ans
9090
```
@@ -96,21 +96,64 @@ class Solution:
9696
```java
9797
class Solution {
9898
public int lengthOfLongestSubstring(String s) {
99-
int i = 0, j = 0, ans = 0;
100-
Set<Character> chars = new HashSet<>();
101-
for (char c : s.toCharArray()) {
102-
while (chars.contains(c)) {
103-
chars.remove(s.charAt(i++));
99+
Set<Character> ss = new HashSet<>();
100+
int i = 0, ans = 0;
101+
for (int j = 0; j < s.length(); ++j) {
102+
char c = s.charAt(j);
103+
while (ss.contains(c)) {
104+
ss.remove(s.charAt(i++));
104105
}
105-
chars.add(c);
106+
ss.add(c);
106107
ans = Math.max(ans, j - i + 1);
107-
++j;
108108
}
109109
return ans;
110110
}
111111
}
112112
```
113113

114+
### **C++**
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
int lengthOfLongestSubstring(string s) {
120+
unordered_set<char> ss;
121+
int i = 0, ans = 0;
122+
for (int j = 0; j < s.size(); ++j) {
123+
while (ss.count(s[j])) ss.erase(s[i++]);
124+
ss.insert(s[j]);
125+
ans = max(ans, j - i + 1);
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
### **Go**
133+
134+
```go
135+
func lengthOfLongestSubstring(s string) int {
136+
ss := map[byte]bool{}
137+
i, ans := 0, 0
138+
for j := 0; j < len(s); j++ {
139+
for ss[s[j]] {
140+
ss[s[i]] = false
141+
i++
142+
}
143+
ss[s[j]] = true
144+
ans = max(ans, j-i+1)
145+
}
146+
return ans
147+
}
148+
149+
func max(a, b int) int {
150+
if a > b {
151+
return a
152+
}
153+
return b
154+
}
155+
```
156+
114157
### **JavaScript**
115158

116159
```js
@@ -119,39 +162,56 @@ class Solution {
119162
* @return {number}
120163
*/
121164
var lengthOfLongestSubstring = function (s) {
122-
let i = 0,
123-
j = 0,
124-
ans = 0;
125-
let chars = new Set();
126-
for (let c of s) {
127-
while (chars.has(c)) {
128-
chars.delete(s[i++]);
165+
const ss = new Set();
166+
let i = 0;
167+
let ans = 0;
168+
for (let j = 0; j < s.length; ++j) {
169+
while (ss.has(s[j])) {
170+
ss.delete(s[i++]);
129171
}
130-
chars.add(c);
172+
ss.add(s[j]);
131173
ans = Math.max(ans, j - i + 1);
132-
++j;
133174
}
134175
return ans;
135176
};
136177
```
137178

179+
### **C#**
180+
181+
```cs
182+
public class Solution {
183+
public int LengthOfLongestSubstring(string s) {
184+
var ss = new HashSet<char>();
185+
int i = 0, ans = 0;
186+
for (int j = 0; j < s.Length; ++j)
187+
{
188+
while (ss.Contains(s[j]))
189+
{
190+
ss.Remove(s[i++]);
191+
}
192+
ss.Add(s[j]);
193+
ans = Math.Max(ans, j - i + 1);
194+
}
195+
return ans;
196+
}
197+
}
198+
```
199+
138200
### **TypeScript**
139201

140202
```ts
141203
function lengthOfLongestSubstring(s: string): number {
142-
// 滑动窗口+哈希表
143-
let left = -1;
144-
let maxLen = 0;
145-
let hashTable = new Map();
146-
for (let right = 0; right < s.length; right++) {
147-
let cur = s.charAt(right);
148-
if (hashTable.has(cur)) {
149-
left = Math.max(left, hashTable.get(cur));
204+
const ss = new Set();
205+
let i = 0;
206+
let ans = 0;
207+
for (let j = 0; j < s.length; ++j) {
208+
while (ss.has(s[j])) {
209+
ss.delete(s[i++]);
150210
}
151-
hashTable.set(cur, right);
152-
maxLen = Math.max(maxLen, right - left);
211+
ss.add(s[j]);
212+
ans = Math.max(ans, j - i + 1);
153213
}
154-
return maxLen;
214+
return ans;
155215
}
156216
```
157217

@@ -179,56 +239,6 @@ class Solution {
179239
}
180240
```
181241

182-
### **Go**
183-
184-
```go
185-
func lengthOfLongestSubstring(s string) int {
186-
window := make(map[byte]int)
187-
n := len(s)
188-
ans := 0
189-
left, right := 0, 0
190-
for right < n {
191-
b := s[right]
192-
right++
193-
window[b]++
194-
for window[b] > 1 {
195-
window[s[left]]--
196-
left++
197-
}
198-
ans = max(ans, right-left)
199-
}
200-
return ans
201-
}
202-
203-
func max(x, y int) int {
204-
if x > y {
205-
return x
206-
}
207-
return y
208-
}
209-
```
210-
211-
### **C++**
212-
213-
```cpp
214-
class Solution {
215-
public:
216-
int lengthOfLongestSubstring(string s) {
217-
int i = 0, j = 0, ans = 0;
218-
unordered_set<char> chars;
219-
for (char& c : s)
220-
{
221-
while (chars.count(c)) chars.erase(s[i++]);
222-
chars.insert(c);
223-
ans = max(ans, j - i + 1);
224-
++j;
225-
}
226-
return ans;
227-
228-
}
229-
};
230-
```
231-
232242
### **Nim**
233243

234244
```nim

0 commit comments

Comments
 (0)