🐌1. 题目描述
:::info 求一个字符串中,最长无重复字符子串长度。 ::: 示例
输入: s = “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。
输入: s = “bbbbb” 输出: 1 解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。
💡2. 解决思路
动态滑动窗口
🚩3. 代码实现
class Solution {public int lengthOfLongestSubstring(String s) {if(s == "" || s==null){return 0;}int max = 0;Set set = new HashSet<Character>();int start = 0;for(int end = 0; end < s.length(); end++){// 不满足条件while(set.contains(s.charAt(end))){set.remove(s.charAt(start));start++;}set.add(s.charAt(end));max = Math.max(end - start + 1,max);}return max;}}
