class Solution2{
public int lengthOfLongestSubstring(String s) {
/*
3. 无重复字符的最长子串
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
*/
Set<Character> set = new HashSet<>();
int lk = 0;
int rk = 0;
int length = s.length();
int tmp = 0;
int max = 0;
while(lk<= rk && rk < length){
while(set.contains(s.charAt(rk))){
set.remove(s.charAt(lk));
lk++;
}
set.add(s.charAt(rk));
rk++;
tmp = set.size();
max = Integer.max(tmp,max);
}
return max;
}
}
【LeetCode】3. 无重复字符的最长子串
最新推荐文章于 2024-09-12 16:31:59 发布