Skip to content

feat: update solutions to lc problem: No.0003 #2396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ for (int i = 0, j = 0; i < n; ++i) {
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ss = set()
i = ans = 0
ans = i = 0
for j, c in enumerate(s):
while c in ss:
ss.remove(s[i])
Expand All @@ -88,14 +88,14 @@ class Solution:
```java
class Solution {
public int lengthOfLongestSubstring(String s) {
Set<Character> ss = new HashSet<>();
int i = 0, ans = 0;
for (int j = 0; j < s.length(); ++j) {
boolean[] ss = new boolean[128];
int ans = 0;
for (int i = 0, j = 0; j < s.length(); ++j) {
char c = s.charAt(j);
while (ss.contains(c)) {
ss.remove(s.charAt(i++));
while (ss[c]) {
ss[s.charAt(i++)] = false;
}
ss.add(c);
ss[c] = true;
ans = Math.max(ans, j - i + 1);
}
return ans;
Expand All @@ -107,11 +107,13 @@ class Solution {
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> ss;
int i = 0, ans = 0;
for (int j = 0; j < s.size(); ++j) {
while (ss.count(s[j])) ss.erase(s[i++]);
ss.insert(s[j]);
bool ss[128]{};
int ans = 0;
for (int i = 0, j = 0; j < s.size(); ++j) {
while (ss[s[j]]) {
ss[s[i++]] = false;
}
ss[s[j]] = true;
ans = max(ans, j - i + 1);
}
return ans;
Expand All @@ -120,31 +122,30 @@ public:
```

```go
func lengthOfLongestSubstring(s string) int {
ss := map[byte]bool{}
i, ans := 0, 0
for j := 0; j < len(s); j++ {
func lengthOfLongestSubstring(s string) (ans int) {
ss := [128]bool{}
for i, j := 0, 0; j < len(s); j++ {
for ss[s[j]] {
ss[s[i]] = false
i++
}
ss[s[j]] = true
ans = max(ans, j-i+1)
}
return ans
return
}
```

```ts
function lengthOfLongestSubstring(s: string): number {
let ans = 0;
const vis = new Set<string>();
for (let i = 0, j = 0; i < s.length; ++i) {
while (vis.has(s[i])) {
vis.delete(s[j++]);
const ss: Set<string> = new Set();
for (let i = 0, j = 0; j < s.length; ++j) {
while (ss.has(s[j])) {
ss.delete(s[i++]);
}
vis.add(s[i]);
ans = Math.max(ans, i - j + 1);
ss.add(s[j]);
ans = Math.max(ans, j - i + 1);
}
return ans;
}
Expand All @@ -156,17 +157,17 @@ use std::collections::HashSet;
impl Solution {
pub fn length_of_longest_substring(s: String) -> i32 {
let s = s.as_bytes();
let mut set = HashSet::new();
let mut ss = HashSet::new();
let mut i = 0;
s
.iter()
.map(|c| {
while set.contains(&c) {
set.remove(&s[i]);
while ss.contains(&c) {
ss.remove(&s[i]);
i += 1;
}
set.insert(c);
set.len()
ss.insert(c);
ss.len()
})
.max()
.unwrap_or(0) as i32
Expand All @@ -180,10 +181,9 @@ impl Solution {
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
const ss = new Set();
let i = 0;
let ans = 0;
for (let j = 0; j < s.length; ++j) {
const ss = new Set();
for (let i = 0, j = 0; j < s.length; ++j) {
while (ss.has(s[j])) {
ss.delete(s[i++]);
}
Expand All @@ -197,12 +197,10 @@ var lengthOfLongestSubstring = function (s) {
```cs
public class Solution {
public int LengthOfLongestSubstring(string s) {
int ans = 0;
var ss = new HashSet<char>();
int i = 0, ans = 0;
for (int j = 0; j < s.Length; ++j)
{
while (ss.Contains(s[j]))
{
for (int i = 0, j = 0; j < s.Length; ++j) {
while (ss.Contains(s[j])) {
ss.Remove(s[i++]);
}
ss.Add(s[j]);
Expand All @@ -220,22 +218,16 @@ class Solution {
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$max = 0;
for ($i = 0; $i < strlen($s); $i++) {
$chars = [];
$sub = '';
for ($j = $i; $j < strlen($s); $j++) {
if (in_array($s[$j], $chars)) {
break;
}
$sub .= $s[$j];
$chars[] = $s[$j];
}
if (strlen($sub) > $max) {
$max = strlen($sub);
$ans = 0;
$ss = [];
for ($i = 0, $j = 0; $j < strlen($s); ++$j) {
while (in_array($s[$j], $ss)) {
unset($ss[array_search($s[$i++], $ss)]);
}
$ss[] = $s[$j];
$ans = max($ans, $j - $i + 1);
}
return $max;
return $ans;
}
}
```
Expand Down Expand Up @@ -284,80 +276,4 @@ proc lengthOfLongestSubstring(s: string): int =

<!-- tabs:end -->

### 方法二

<!-- tabs:start -->

```java
class Solution {
public int lengthOfLongestSubstring(String s) {
boolean[] ss = new boolean[128];
int ans = 0, j = 0;
int n = s.length();
for (int i = 0; i < n; ++i) {
char c = s.charAt(i);
while (ss[c]) {
ss[s.charAt(j++)] = false;
}
ans = Math.max(ans, i - j + 1);
ss[c] = true;
}
return ans;
}
}
```

```cpp
class Solution {
public:
int lengthOfLongestSubstring(string s) {
bool ss[128] = {false};
int n = s.size();
int ans = 0;
for (int i = 0, j = 0; i < n; ++i) {
while (ss[s[i]]) {
ss[s[j++]] = false;
}
ss[s[i]] = true;
ans = max(ans, i - j + 1);
}
return ans;
}
};
```

```go
func lengthOfLongestSubstring(s string) (ans int) {
ss := make([]bool, 128)
j := 0
for i, c := range s {
for ss[c] {
ss[s[j]] = false
j++
}
ss[c] = true
ans = max(ans, i-j+1)
}
return
}
```

```ts
function lengthOfLongestSubstring(s: string): number {
let ans = 0;
const n = s.length;
const ss: boolean[] = new Array(128).fill(false);
for (let i = 0, j = 0; i < n; ++i) {
while (ss[s[i]]) {
ss[s[j++]] = false;
}
ss[s[i]] = true;
ans = Math.max(ans, i - j + 1);
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Loading