Skip to content

Commit a9fa871

Browse files
committed
feat: update solutions to lc/lcof problem: Longest Substring Without
Repeating Characters
1 parent d946c12 commit a9fa871

File tree

11 files changed

+244
-247
lines changed

11 files changed

+244
-247
lines changed

lcof/面试题48. 最长不含重复字符的子字符串/README.md

+72-91
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,16 @@
4242
```python
4343
class Solution:
4444
def lengthOfLongestSubstring(self, s: str) -> int:
45-
if not s:
46-
return 0
47-
cache = {}
48-
cache[s[0]] = 0
49-
dp = [0 for _ in s]
50-
dp[0] = res = 1
51-
for i in range(1, len(s)):
52-
if s[i] == s[i - 1]:
53-
dp[i] = 1
54-
else:
55-
if cache.get(s[i]) is None:
56-
dp[i] = dp[i - 1] + 1
57-
else:
58-
dp[i] = min(dp[i - 1] + 1, i - cache[s[i]])
59-
cache[s[i]] = i
60-
res = max(res, dp[i])
45+
i = j = res = 0
46+
chars = set()
47+
while i < len(s):
48+
while s[i] in chars:
49+
if s[j] in chars:
50+
chars.remove(s[j])
51+
j += 1
52+
chars.add(s[i])
53+
res = max(res, i - j + 1)
54+
i += 1
6155
return res
6256
```
6357

@@ -66,100 +60,87 @@ class Solution:
6660
```java
6761
class Solution {
6862
public int lengthOfLongestSubstring(String s) {
69-
if (s == null || "".equals(s)) {
70-
return 0;
71-
}
72-
int n = s.length();
73-
char[] chars = s.toCharArray();
74-
int[] dp = new int[n];
75-
int res = 1;
76-
Map<Character, Integer> map = new HashMap<>();
77-
dp[0] = 1;
78-
map.put(chars[0], 0);
79-
for (int i = 1; i < n; ++i) {
80-
if (chars[i] == chars[i - 1]) {
81-
dp[i] = 1;
82-
} else {
83-
if (map.get(chars[i]) == null) {
84-
dp[i] = dp[i - 1] + 1;
85-
} else {
86-
dp[i] = Math.min(dp[i - 1] + 1, i - map.get(chars[i]));
87-
}
63+
int res = 0;
64+
Set<Character> set = new HashSet<>();
65+
for (int i = 0, j = 0; i < s.length(); ++i) {
66+
char c = s.charAt(i);
67+
while (set.contains(c)) {
68+
set.remove(s.charAt(j++));
8869
}
89-
map.put(chars[i], i);
90-
res = Math.max(res, dp[i]);
70+
set.add(c);
71+
res = Math.max(res, i - j + 1);
9172
}
9273
return res;
9374
}
9475
}
9576
```
9677

97-
### **JavaScript**
98-
99-
```js
100-
/**
101-
* @param {string} s
102-
* @return {number}
103-
*/
104-
var lengthOfLongestSubstring = function (s) {
105-
let left = 0;
106-
let right = 0;
107-
let res = 0;
108-
let len = s.length;
109-
let rec = {};
110-
while (right < len) {
111-
let tmp = "*";
112-
while (right < len) {
113-
tmp = s[right];
114-
if (!rec[tmp]) rec[tmp] = 0;
115-
rec[tmp]++;
116-
if (rec[tmp] > 1) break;
117-
right++;
118-
}
119-
res = Math.max(res, right - left);
120-
while (rec[tmp] > 1) rec[s[left++]]--;
121-
right++;
122-
}
123-
return res;
124-
};
125-
```
126-
12778
### **C++**
12879

12980
```cpp
13081
class Solution {
13182
public:
13283
int lengthOfLongestSubstring(string s) {
133-
int arr[1024]; // 本题的用例中,有不为小写字母的情况
134-
for (int i = 0; i < 1024; i++) {
135-
arr[i] = -1;
84+
int res = 0;
85+
unordered_set<char> chars;
86+
for (int i = 0, j = 0; i < s.size(); ++i)
87+
{
88+
while (chars.count(s[i]))
89+
{
90+
chars.erase(s[j++]);
91+
}
92+
chars.insert(s[i]);
93+
res = max(res, i - j + 1);
13694
}
95+
return res;
96+
}
97+
};
98+
```
13799
138-
int curLen = 0;
139-
int maxLen = 0;
140-
141-
int len = s.size();
142-
for (int i = 0; i < len; i++) {
143-
int prev = arr[int(s[i])]; // 之前位置的index
144-
if (prev < 0 || i - prev > curLen) {
145-
// 其中,prev>0表示之前没有遇到过该字符
146-
// i - prev > curLen 表示之前遇到的当前字符,远超当前限定的范围
147-
// 这两种情况下,都是直接继续加就可以了
148-
curLen++;
149-
} else {
150-
if (curLen > maxLen) {
151-
maxLen = curLen;
152-
}
153-
curLen = i - prev; // curLen重新开始计数
154-
}
100+
### **Go**
101+
102+
```go
103+
func lengthOfLongestSubstring(s string) int {
104+
chars := make(map[byte]bool)
105+
res := 0
106+
for i, j := 0, 0; i < len(s); i++ {
107+
for chars[s[i]] {
108+
chars[s[j]] = false
109+
j++
110+
}
111+
chars[s[i]] = true
112+
res = max(res, i-j+1)
113+
}
114+
return res
115+
}
155116
156-
arr[int(s[i])] = i;
157-
}
117+
func max(a, b int) int {
118+
if a > b {
119+
return a
120+
}
121+
return b
122+
}
123+
```
124+
125+
### **JavaScript**
158126

159-
return maxLen > curLen ? maxLen : curLen;
127+
```js
128+
/**
129+
* @param {string} s
130+
* @return {number}
131+
*/
132+
var lengthOfLongestSubstring = function(s) {
133+
let res = 0;
134+
let chars = new Set();
135+
for (let i = 0, j = 0; i < s.length; ++i) {
136+
while (chars.has(s[i])) {
137+
chars.delete(s[j++]);
138+
}
139+
chars.add(s[i]);
140+
res = Math.max(res, i - j + 1);
160141
}
142+
return res;
161143
};
162-
163144
```
164145

165146
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
class Solution {
22
public:
33
int lengthOfLongestSubstring(string s) {
4-
int arr[1024]; // 本题的用例中,有不为小写字母的情况
5-
for (int i = 0; i < 1024; i++) {
6-
arr[i] = -1;
7-
}
8-
9-
int curLen = 0;
10-
int maxLen = 0;
11-
12-
int len = s.size();
13-
for (int i = 0; i < len; i++) {
14-
int prev = arr[int(s[i])]; // 之前位置的index
15-
if (prev < 0 || i - prev > curLen) {
16-
// 其中,prev>0表示之前没有遇到过该字符
17-
// i - prev > curLen 表示之前遇到的当前字符,远超当前限定的范围
18-
// 这两种情况下,都是直接继续加就可以了
19-
curLen++;
20-
} else {
21-
if (curLen > maxLen) {
22-
maxLen = curLen;
23-
}
24-
curLen = i - prev; // curLen重新开始计数
4+
int res = 0;
5+
unordered_set<char> chars;
6+
for (int i = 0, j = 0; i < s.size(); ++i)
7+
{
8+
while (chars.count(s[i]))
9+
{
10+
chars.erase(s[j++]);
2511
}
26-
27-
arr[int(s[i])] = i;
12+
chars.insert(s[i]);
13+
res = max(res, i - j + 1);
2814
}
29-
30-
return maxLen > curLen ? maxLen : curLen;
15+
return res;
3116
}
32-
};
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func lengthOfLongestSubstring(s string) int {
2+
chars := make(map[byte]bool)
3+
res := 0
4+
for i, j := 0, 0; i < len(s); i++ {
5+
for chars[s[i]] {
6+
chars[s[j]] = false
7+
j++
8+
}
9+
chars[s[i]] = true
10+
res = max(res, i-j+1)
11+
}
12+
return res
13+
}
14+
15+
func max(a, b int) int {
16+
if a > b {
17+
return a
18+
}
19+
return b
20+
}

lcof/面试题48. 最长不含重复字符的子字符串/Solution.java

+8-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
class Solution {
22
public int lengthOfLongestSubstring(String s) {
3-
if (s == null || "".equals(s)) {
4-
return 0;
5-
}
6-
int n = s.length();
7-
char[] chars = s.toCharArray();
8-
int[] dp = new int[n];
9-
int res = 1;
10-
Map<Character, Integer> map = new HashMap<>();
11-
dp[0] = 1;
12-
map.put(chars[0], 0);
13-
for (int i = 1; i < n; ++i) {
14-
if (chars[i] == chars[i - 1]) {
15-
dp[i] = 1;
16-
} else {
17-
if (map.get(chars[i]) == null) {
18-
dp[i] = dp[i - 1] + 1;
19-
} else {
20-
dp[i] = Math.min(dp[i - 1] + 1, i - map.get(chars[i]));
21-
}
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++));
229
}
23-
map.put(chars[i], i);
24-
res = Math.max(res, dp[i]);
10+
set.add(c);
11+
res = Math.max(res, i - j + 1);
2512
}
2613
return res;
2714
}

lcof/面试题48. 最长不含重复字符的子字符串/Solution.js

+11-20
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22
* @param {string} s
33
* @return {number}
44
*/
5-
var lengthOfLongestSubstring = function (s) {
6-
let left = 0;
7-
let right = 0;
8-
let res = 0;
9-
let len = s.length;
10-
let rec = {};
11-
while (right < len) {
12-
let tmp = "*";
13-
while (right < len) {
14-
tmp = s[right];
15-
if (!rec[tmp]) rec[tmp] = 0;
16-
rec[tmp]++;
17-
if (rec[tmp] > 1) break;
18-
right++;
5+
var lengthOfLongestSubstring = function(s) {
6+
let res = 0;
7+
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++]);
11+
}
12+
chars.add(s[i]);
13+
res = Math.max(res, i - j + 1);
1914
}
20-
res = Math.max(res, right - left);
21-
while (rec[tmp] > 1) rec[s[left++]]--;
22-
right++;
23-
}
24-
return res;
25-
};
15+
return res;
16+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
class Solution:
22
def lengthOfLongestSubstring(self, s: str) -> int:
3-
if not s:
4-
return 0
5-
cache = {}
6-
cache[s[0]] = 0
7-
dp = [0 for _ in s]
8-
dp[0] = res = 1
9-
for i in range(1, len(s)):
10-
if s[i] == s[i - 1]:
11-
dp[i] = 1
12-
else:
13-
if cache.get(s[i]) is None:
14-
dp[i] = dp[i - 1] + 1
15-
else:
16-
dp[i] = min(dp[i - 1] + 1, i - cache[s[i]])
17-
cache[s[i]] = i
18-
res = max(res, dp[i])
19-
return res
3+
i = j = res = 0
4+
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

0 commit comments

Comments
 (0)