更新 Solution.java
parent
1a1f4b723f
commit
da81823464
|
|
@ -5,13 +5,24 @@ import java.util.Set;
|
||||||
|
|
||||||
public class Solution {
|
public class Solution {
|
||||||
public int lengthOfLongestSubstring(String s) {
|
public int lengthOfLongestSubstring(String s) {
|
||||||
if (s != null){
|
// 哈希集合,记录每个字符是否出现过
|
||||||
Set<Character> stringList = new HashSet<>();
|
Set<Character> occ = new HashSet<Character>();
|
||||||
for (int i = 0; i < s.length(); i++) {
|
int n = s.length();
|
||||||
stringList.add(s.charAt(i));
|
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
|
||||||
|
int rk = -1, ans = 0;
|
||||||
|
for (int i = 0; i < n; ++i) {
|
||||||
|
if (i != 0) {
|
||||||
|
// 左指针向右移动一格,移除一个字符
|
||||||
|
occ.remove(s.charAt(i - 1));
|
||||||
}
|
}
|
||||||
return stringList.size();
|
while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
|
||||||
|
// 不断地移动右指针
|
||||||
|
occ.add(s.charAt(rk + 1));
|
||||||
|
++rk;
|
||||||
|
}
|
||||||
|
// 第 i 到 rk 个字符是一个极长的无重复字符子串
|
||||||
|
ans = Math.max(ans, rk - i + 1);
|
||||||
}
|
}
|
||||||
return 0;
|
return ans;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue