feat: 2025-10-23打卡
This commit is contained in:
39
3.longest-substring-without-repeating-characters.java
Normal file
39
3.longest-substring-without-repeating-characters.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* @lc app=leetcode id=3 lang=java
|
||||
*
|
||||
* [3] Longest Substring Without Repeating Characters
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int lengthOfLongestSubstring(String s) {
|
||||
if(s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
Set<Character> chs = new HashSet<>();
|
||||
int i = 0, j = 0, r = 0, cur = 0;
|
||||
while(j < s.length()) {
|
||||
if(!chs.contains(s.charAt(j))) {
|
||||
chs.add(s.charAt(j));
|
||||
cur++;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
} else {
|
||||
while(s.charAt(i) != s.charAt(j)) {
|
||||
chs.remove(s.charAt(i));
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
cur = j - i + 1;
|
||||
if(cur > r) {
|
||||
r = cur;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user