LeetCode/com/zerroi/leetcode/Three15/LengthOfLongestSubstring.java

35 lines
826 B
Java
Raw Normal View History

2024-03-21 19:03:41 +08:00
package com.zerroi.leetcode.Three15;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/*给定一个字符串 s 请你找出其中不含有重复字符的 最长
子串
的长度*/
public class LengthOfLongestSubstring {
public static void main(String[] args) {
}
}
class SolutionSecond {
public int lengthOfLongestSubstring(String s) {
Set<Character> set = new HashSet<>();
int start = 0;
int end = 0;
int res = 0;
while (end < s.length()) {
if (!set.contains(s.charAt(end))) {
set.add(s.charAt(end));
end++;
} else {
set.remove(s.charAt(start));
start++;
}
res = Math.max(res, set.size());
}
return res;
}
}