forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lcof problem: No.48
- Loading branch information
Showing
8 changed files
with
126 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
class Solution { | ||
public: | ||
int lengthOfLongestSubstring(string s) { | ||
int res = 0; | ||
unordered_set<char> chars; | ||
int ans = 0; | ||
unordered_set<char> vis; | ||
for (int i = 0, j = 0; i < s.size(); ++i) { | ||
while (chars.count(s[i])) { | ||
chars.erase(s[j++]); | ||
while (vis.count(s[i])) { | ||
vis.erase(s[j++]); | ||
} | ||
chars.insert(s[i]); | ||
res = max(res, i - j + 1); | ||
vis.insert(s[i]); | ||
ans = max(ans, i - j + 1); | ||
} | ||
return res; | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
class Solution { | ||
public int lengthOfLongestSubstring(String s) { | ||
int res = 0; | ||
Set<Character> set = new HashSet<>(); | ||
for (int i = 0, j = 0; i < s.length(); ++i) { | ||
char c = s.charAt(i); | ||
while (set.contains(c)) { | ||
set.remove(s.charAt(j++)); | ||
int ans = 0, j = 0; | ||
Set<Character> vis = new HashSet<>(); | ||
for (int i = 0; i < s.length(); ++i) { | ||
while (vis.contains(s.charAt(i))) { | ||
vis.remove(s.charAt(j++)); | ||
} | ||
set.add(c); | ||
res = Math.max(res, i - j + 1); | ||
vis.add(s.charAt(i)); | ||
ans = Math.max(ans, i - j + 1); | ||
} | ||
return res; | ||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
i = j = res = 0 | ||
chars = set() | ||
while i < len(s): | ||
while s[i] in chars: | ||
if s[j] in chars: | ||
chars.remove(s[j]) | ||
vis = set() | ||
ans = j = 0 | ||
for i, c in enumerate(s): | ||
while c in vis: | ||
vis.remove(s[j]) | ||
j += 1 | ||
chars.add(s[i]) | ||
res = max(res, i - j + 1) | ||
i += 1 | ||
return res | ||
vis.add(c) | ||
ans = max(ans, i - j + 1) | ||
return ans |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
function lengthOfLongestSubstring(s: string): number { | ||
const n = s.length; | ||
const set = new Set<string>(); | ||
let res = 0; | ||
let i = 0; | ||
for (let j = 0; j < n; j++) { | ||
const c = s[j]; | ||
while (set.has(c)) { | ||
set.delete(s[i++]); | ||
let ans = 0; | ||
let vis = new Set<string>(); | ||
for (let i = 0, j = 0; i < s.length; ++i) { | ||
while (vis.has(s[i])) { | ||
vis.delete(s[j++]); | ||
} | ||
set.add(c); | ||
res = Math.max(res, set.size); | ||
vis.add(s[i]); | ||
ans = Math.max(ans, i - j + 1); | ||
} | ||
return res; | ||
return ans; | ||
} |