Skip to content

Commit 0b860fd

Browse files
authored
feat: update solutions to lc problem: No.0003 (#2396)
No.0003.Longest Substring Without Repeating Characters
1 parent b657b11 commit 0b860fd

File tree

15 files changed

+127
-361
lines changed

15 files changed

+127
-361
lines changed

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

Lines changed: 42 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ for (int i = 0, j = 0; i < n; ++i) {
7575
class Solution:
7676
def lengthOfLongestSubstring(self, s: str) -> int:
7777
ss = set()
78-
i = ans = 0
78+
ans = i = 0
7979
for j, c in enumerate(s):
8080
while c in ss:
8181
ss.remove(s[i])
@@ -88,14 +88,14 @@ class Solution:
8888
```java
8989
class Solution {
9090
public int lengthOfLongestSubstring(String s) {
91-
Set<Character> ss = new HashSet<>();
92-
int i = 0, ans = 0;
93-
for (int j = 0; j < s.length(); ++j) {
91+
boolean[] ss = new boolean[128];
92+
int ans = 0;
93+
for (int i = 0, j = 0; j < s.length(); ++j) {
9494
char c = s.charAt(j);
95-
while (ss.contains(c)) {
96-
ss.remove(s.charAt(i++));
95+
while (ss[c]) {
96+
ss[s.charAt(i++)] = false;
9797
}
98-
ss.add(c);
98+
ss[c] = true;
9999
ans = Math.max(ans, j - i + 1);
100100
}
101101
return ans;
@@ -107,11 +107,13 @@ class Solution {
107107
class Solution {
108108
public:
109109
int lengthOfLongestSubstring(string s) {
110-
unordered_set<char> ss;
111-
int i = 0, ans = 0;
112-
for (int j = 0; j < s.size(); ++j) {
113-
while (ss.count(s[j])) ss.erase(s[i++]);
114-
ss.insert(s[j]);
110+
bool ss[128]{};
111+
int ans = 0;
112+
for (int i = 0, j = 0; j < s.size(); ++j) {
113+
while (ss[s[j]]) {
114+
ss[s[i++]] = false;
115+
}
116+
ss[s[j]] = true;
115117
ans = max(ans, j - i + 1);
116118
}
117119
return ans;
@@ -120,31 +122,30 @@ public:
120122
```
121123
122124
```go
123-
func lengthOfLongestSubstring(s string) int {
124-
ss := map[byte]bool{}
125-
i, ans := 0, 0
126-
for j := 0; j < len(s); j++ {
125+
func lengthOfLongestSubstring(s string) (ans int) {
126+
ss := [128]bool{}
127+
for i, j := 0, 0; j < len(s); j++ {
127128
for ss[s[j]] {
128129
ss[s[i]] = false
129130
i++
130131
}
131132
ss[s[j]] = true
132133
ans = max(ans, j-i+1)
133134
}
134-
return ans
135+
return
135136
}
136137
```
137138

138139
```ts
139140
function lengthOfLongestSubstring(s: string): number {
140141
let ans = 0;
141-
const vis = new Set<string>();
142-
for (let i = 0, j = 0; i < s.length; ++i) {
143-
while (vis.has(s[i])) {
144-
vis.delete(s[j++]);
142+
const ss: Set<string> = new Set();
143+
for (let i = 0, j = 0; j < s.length; ++j) {
144+
while (ss.has(s[j])) {
145+
ss.delete(s[i++]);
145146
}
146-
vis.add(s[i]);
147-
ans = Math.max(ans, i - j + 1);
147+
ss.add(s[j]);
148+
ans = Math.max(ans, j - i + 1);
148149
}
149150
return ans;
150151
}
@@ -156,17 +157,17 @@ use std::collections::HashSet;
156157
impl Solution {
157158
pub fn length_of_longest_substring(s: String) -> i32 {
158159
let s = s.as_bytes();
159-
let mut set = HashSet::new();
160+
let mut ss = HashSet::new();
160161
let mut i = 0;
161162
s
162163
.iter()
163164
.map(|c| {
164-
while set.contains(&c) {
165-
set.remove(&s[i]);
165+
while ss.contains(&c) {
166+
ss.remove(&s[i]);
166167
i += 1;
167168
}
168-
set.insert(c);
169-
set.len()
169+
ss.insert(c);
170+
ss.len()
170171
})
171172
.max()
172173
.unwrap_or(0) as i32
@@ -180,10 +181,9 @@ impl Solution {
180181
* @return {number}
181182
*/
182183
var lengthOfLongestSubstring = function (s) {
183-
const ss = new Set();
184-
let i = 0;
185184
let ans = 0;
186-
for (let j = 0; j < s.length; ++j) {
185+
const ss = new Set();
186+
for (let i = 0, j = 0; j < s.length; ++j) {
187187
while (ss.has(s[j])) {
188188
ss.delete(s[i++]);
189189
}
@@ -197,12 +197,10 @@ var lengthOfLongestSubstring = function (s) {
197197
```cs
198198
public class Solution {
199199
public int LengthOfLongestSubstring(string s) {
200+
int ans = 0;
200201
var ss = new HashSet<char>();
201-
int i = 0, ans = 0;
202-
for (int j = 0; j < s.Length; ++j)
203-
{
204-
while (ss.Contains(s[j]))
205-
{
202+
for (int i = 0, j = 0; j < s.Length; ++j) {
203+
while (ss.Contains(s[j])) {
206204
ss.Remove(s[i++]);
207205
}
208206
ss.Add(s[j]);
@@ -220,22 +218,16 @@ class Solution {
220218
* @return Integer
221219
*/
222220
function lengthOfLongestSubstring($s) {
223-
$max = 0;
224-
for ($i = 0; $i < strlen($s); $i++) {
225-
$chars = [];
226-
$sub = '';
227-
for ($j = $i; $j < strlen($s); $j++) {
228-
if (in_array($s[$j], $chars)) {
229-
break;
230-
}
231-
$sub .= $s[$j];
232-
$chars[] = $s[$j];
233-
}
234-
if (strlen($sub) > $max) {
235-
$max = strlen($sub);
221+
$ans = 0;
222+
$ss = [];
223+
for ($i = 0, $j = 0; $j < strlen($s); ++$j) {
224+
while (in_array($s[$j], $ss)) {
225+
unset($ss[array_search($s[$i++], $ss)]);
236226
}
227+
$ss[] = $s[$j];
228+
$ans = max($ans, $j - $i + 1);
237229
}
238-
return $max;
230+
return $ans;
239231
}
240232
}
241233
```
@@ -284,80 +276,4 @@ proc lengthOfLongestSubstring(s: string): int =
284276

285277
<!-- tabs:end -->
286278

287-
### 方法二
288-
289-
<!-- tabs:start -->
290-
291-
```java
292-
class Solution {
293-
public int lengthOfLongestSubstring(String s) {
294-
boolean[] ss = new boolean[128];
295-
int ans = 0, j = 0;
296-
int n = s.length();
297-
for (int i = 0; i < n; ++i) {
298-
char c = s.charAt(i);
299-
while (ss[c]) {
300-
ss[s.charAt(j++)] = false;
301-
}
302-
ans = Math.max(ans, i - j + 1);
303-
ss[c] = true;
304-
}
305-
return ans;
306-
}
307-
}
308-
```
309-
310-
```cpp
311-
class Solution {
312-
public:
313-
int lengthOfLongestSubstring(string s) {
314-
bool ss[128] = {false};
315-
int n = s.size();
316-
int ans = 0;
317-
for (int i = 0, j = 0; i < n; ++i) {
318-
while (ss[s[i]]) {
319-
ss[s[j++]] = false;
320-
}
321-
ss[s[i]] = true;
322-
ans = max(ans, i - j + 1);
323-
}
324-
return ans;
325-
}
326-
};
327-
```
328-
329-
```go
330-
func lengthOfLongestSubstring(s string) (ans int) {
331-
ss := make([]bool, 128)
332-
j := 0
333-
for i, c := range s {
334-
for ss[c] {
335-
ss[s[j]] = false
336-
j++
337-
}
338-
ss[c] = true
339-
ans = max(ans, i-j+1)
340-
}
341-
return
342-
}
343-
```
344-
345-
```ts
346-
function lengthOfLongestSubstring(s: string): number {
347-
let ans = 0;
348-
const n = s.length;
349-
const ss: boolean[] = new Array(128).fill(false);
350-
for (let i = 0, j = 0; i < n; ++i) {
351-
while (ss[s[i]]) {
352-
ss[s[j++]] = false;
353-
}
354-
ss[s[i]] = true;
355-
ans = Math.max(ans, i - j + 1);
356-
}
357-
return ans;
358-
}
359-
```
360-
361-
<!-- tabs:end -->
362-
363279
<!-- end -->

0 commit comments

Comments
 (0)