Skip to content

Commit cbeab4e

Browse files
thinkasanyyanglbme
andauthored
feat: update solutions to lc/lcof2 problems (#1473)
lc No.0003 & lcof2 No.016 & lcof No.48. Longest Substring Without Repeating Characters --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 84d4894 commit cbeab4e

File tree

8 files changed

+274
-31
lines changed

8 files changed

+274
-31
lines changed

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

+79-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,25 @@ class Solution {
101101
}
102102
```
103103

104+
```java
105+
class Solution {
106+
public int lengthOfLongestSubstring(String s) {
107+
boolean[] ss = new boolean[128];
108+
int ans = 0, j = 0;
109+
int n = s.length();
110+
for (int i = 0; i < n; ++i) {
111+
char c = s.charAt(i);
112+
while (ss[c]) {
113+
ss[s.charAt(j++)] = false;
114+
}
115+
ans = Math.max(ans, i - j + 1);
116+
ss[c] = true;
117+
}
118+
return ans;
119+
}
120+
}
121+
```
122+
104123
### **C++**
105124

106125
```cpp
@@ -121,6 +140,25 @@ public:
121140
};
122141
```
123142
143+
```cpp
144+
class Solution {
145+
public:
146+
int lengthOfLongestSubstring(string s) {
147+
bool ss[128] = {false};
148+
int n = s.size();
149+
int ans = 0;
150+
for (int i = 0, j = 0; i < n; ++i) {
151+
while (ss[s[i]]) {
152+
ss[s[j++]] = false;
153+
}
154+
ss[s[i]] = true;
155+
ans = max(ans, i - j + 1);
156+
}
157+
return ans;
158+
}
159+
};
160+
```
161+
124162
### **Go**
125163

126164
```go
@@ -146,6 +184,29 @@ func max(a, b int) int {
146184
}
147185
```
148186

187+
```go
188+
func lengthOfLongestSubstring(s string) (ans int) {
189+
ss := make([]bool, 128)
190+
j := 0
191+
for i, c := range s {
192+
for ss[c] {
193+
ss[s[j]] = false
194+
j++
195+
}
196+
ss[c] = true
197+
ans = max(ans, i-j+1)
198+
}
199+
return
200+
}
201+
202+
func max(a, b int) int {
203+
if a > b {
204+
return a
205+
}
206+
return b
207+
}
208+
```
209+
149210
### **JavaScript**
150211

151212
```js
@@ -155,7 +216,7 @@ func max(a, b int) int {
155216
*/
156217
var lengthOfLongestSubstring = function (s) {
157218
let ans = 0;
158-
let vis = new Set();
219+
const vis = new Set();
159220
for (let i = 0, j = 0; i < s.length; ++i) {
160221
while (vis.has(s[i])) {
161222
vis.delete(s[j++]);
@@ -172,7 +233,7 @@ var lengthOfLongestSubstring = function (s) {
172233
```ts
173234
function lengthOfLongestSubstring(s: string): number {
174235
let ans = 0;
175-
let vis = new Set<string>();
236+
const vis = new Set<string>();
176237
for (let i = 0, j = 0; i < s.length; ++i) {
177238
while (vis.has(s[i])) {
178239
vis.delete(s[j++]);
@@ -184,6 +245,22 @@ function lengthOfLongestSubstring(s: string): number {
184245
}
185246
```
186247

248+
```ts
249+
function lengthOfLongestSubstring(s: string): number {
250+
let ans = 0;
251+
const n = s.length;
252+
const ss: boolean[] = new Array(128).fill(false);
253+
for (let i = 0, j = 0; i < n; ++i) {
254+
while (ss[s[i]]) {
255+
ss[s[j++]] = false;
256+
}
257+
ss[s[i]] = true;
258+
ans = Math.max(ans, i - j + 1);
259+
}
260+
return ans;
261+
}
262+
```
263+
187264
### **Rust**
188265

189266
```rust

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
var lengthOfLongestSubstring = function (s) {
66
let ans = 0;
7-
let vis = new Set();
7+
const vis = new Set();
88
for (let i = 0, j = 0; i < s.length; ++i) {
99
while (vis.has(s[i])) {
1010
vis.delete(s[j++]);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function lengthOfLongestSubstring(s: string): number {
22
let ans = 0;
3-
let vis = new Set<string>();
3+
const vis = new Set<string>();
44
for (let i = 0, j = 0; i < s.length; ++i) {
55
while (vis.has(s[i])) {
66
vis.delete(s[j++]);

lcof2/剑指 Offer II 016. 不含重复字符的最长子字符串/README.md

+18-3
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,31 @@ func max(a, b int) int {
156156

157157
### **TypeScript**
158158

159+
```ts
160+
function lengthOfLongestSubstring(s: string): number {
161+
let ans = 0;
162+
const vis = new Set<string>();
163+
for (let i = 0, j = 0; i < s.length; ++i) {
164+
while (vis.has(s[i])) {
165+
vis.delete(s[j++]);
166+
}
167+
vis.add(s[i]);
168+
ans = Math.max(ans, i - j + 1);
169+
}
170+
return ans;
171+
}
172+
```
173+
159174
```ts
160175
function lengthOfLongestSubstring(s: string): number {
161176
let ans = 0;
162177
const n = s.length;
163178
const ss: boolean[] = new Array(128).fill(false);
164179
for (let i = 0, j = 0; i < n; ++i) {
165-
while (ss[s.charCodeAt(i)]) {
166-
ss[s.charCodeAt(j++)] = false;
180+
while (ss[s[i]]) {
181+
ss[s[j++]] = false;
167182
}
168-
ss[s.charCodeAt(i)] = true;
183+
ss[s[i]] = true;
169184
ans = Math.max(ans, i - j + 1);
170185
}
171186
return ans;

lcof2/剑指 Offer II 016. 不含重复字符的最长子字符串/Solution.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ function lengthOfLongestSubstring(s: string): number {
33
const n = s.length;
44
const ss: boolean[] = new Array(128).fill(false);
55
for (let i = 0, j = 0; i < n; ++i) {
6-
while (ss[s.charCodeAt(i)]) {
7-
ss[s.charCodeAt(j++)] = false;
6+
while (ss[s[i]]) {
7+
ss[s[j++]] = false;
88
}
9-
ss[s.charCodeAt(i)] = true;
9+
ss[s[i]] = true;
1010
ans = Math.max(ans, i - j + 1);
1111
}
1212
return ans;

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

+83-7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ class Solution {
111111
}
112112
```
113113

114+
```java
115+
class Solution {
116+
public int lengthOfLongestSubstring(String s) {
117+
boolean[] ss = new boolean[128];
118+
int ans = 0, j = 0;
119+
int n = s.length();
120+
for (int i = 0; i < n; ++i) {
121+
char c = s.charAt(i);
122+
while (ss[c]) {
123+
ss[s.charAt(j++)] = false;
124+
}
125+
ans = Math.max(ans, i - j + 1);
126+
ss[c] = true;
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
114133
### **C++**
115134

116135
```cpp
@@ -129,6 +148,25 @@ public:
129148
};
130149
```
131150
151+
```cpp
152+
class Solution {
153+
public:
154+
int lengthOfLongestSubstring(string s) {
155+
bool ss[128] = {false};
156+
int n = s.size();
157+
int ans = 0;
158+
for (int i = 0, j = 0; i < n; ++i) {
159+
while (ss[s[i]]) {
160+
ss[s[j++]] = false;
161+
}
162+
ss[s[i]] = true;
163+
ans = max(ans, i - j + 1);
164+
}
165+
return ans;
166+
}
167+
};
168+
```
169+
132170
### **Go**
133171

134172
```go
@@ -154,6 +192,29 @@ func max(a, b int) int {
154192
}
155193
```
156194

195+
```go
196+
func lengthOfLongestSubstring(s string) (ans int) {
197+
ss := make([]bool, 128)
198+
j := 0
199+
for i, c := range s {
200+
for ss[c] {
201+
ss[s[j]] = false
202+
j++
203+
}
204+
ss[c] = true
205+
ans = max(ans, i-j+1)
206+
}
207+
return
208+
}
209+
210+
func max(a, b int) int {
211+
if a > b {
212+
return a
213+
}
214+
return b
215+
}
216+
```
217+
157218
### **JavaScript**
158219

159220
```js
@@ -201,15 +262,30 @@ public class Solution {
201262

202263
```ts
203264
function lengthOfLongestSubstring(s: string): number {
204-
const ss = new Set();
205-
let i = 0;
206265
let ans = 0;
207-
for (let j = 0; j < s.length; ++j) {
208-
while (ss.has(s[j])) {
209-
ss.delete(s[i++]);
266+
const vis = new Set<string>();
267+
for (let i = 0, j = 0; i < s.length; ++i) {
268+
while (vis.has(s[i])) {
269+
vis.delete(s[j++]);
210270
}
211-
ss.add(s[j]);
212-
ans = Math.max(ans, j - i + 1);
271+
vis.add(s[i]);
272+
ans = Math.max(ans, i - j + 1);
273+
}
274+
return ans;
275+
}
276+
```
277+
278+
```ts
279+
function lengthOfLongestSubstring(s: string): number {
280+
let ans = 0;
281+
const n = s.length;
282+
const ss: boolean[] = new Array(128).fill(false);
283+
for (let i = 0, j = 0; i < n; ++i) {
284+
while (ss[s[i]]) {
285+
ss[s[j++]] = false;
286+
}
287+
ss[s[i]] = true;
288+
ans = Math.max(ans, i - j + 1);
213289
}
214290
return ans;
215291
}

0 commit comments

Comments
 (0)