leetcode-05 8. 无重复字符的最长子串https://leetcode.cn/problems/longest-substring-without-repeating-characters/给定一个字符串s请你找出其中不含有重复字符的最长 子串的长度。示例 1:输入:s abcabcbb输出:3解释:因为无重复字符的最长子串是abc所以其长度为 3。注意 bca 和 cab 也是正确答案。示例 2:输入:s bbbbb输出:1解释:因为无重复字符的最长子串是b所以其长度为 1。示例 3:输入:s pwwkew输出:3解释:因为无重复字符的最长子串是wke所以其长度为 3。 请注意你的答案必须是子串的长度pwke是一个子序列不是子串。提示0 s.length 5 * 104s由英文字母、数字、符号和空格组成1.动态窗口class Solution { public int lengthOfLongestSubstring(String s) { HashSetCharacter set new HashSet(); int ns.length(); int max0; int rk-1; for(int i0;in;i){ if(i!0){ set.remove(s.charAt(i-1)); } while(rk1n !set.contains(s.charAt(rk1))){ set.add(s.charAt(rk1)); rk; } maxMath.max(max,rk-i1); } return max; } }9. 找到字符串中所有字母异位词https://leetcode.cn/problems/find-all-anagrams-in-a-string/给定两个字符串s和p找到s中所有p的异位词的子串返回这些子串的起始索引。不考虑答案输出的顺序。示例 1:输入:s cbaebabacd, p abc输出:[0,6]解释:起始索引等于 0 的子串是 cba, 它是 abc 的异位词。 起始索引等于 6 的子串是 bac, 它是 abc 的异位词。示例 2:输入:s abab, p ab输出:[0,1,2]解释:起始索引等于 0 的子串是 ab, 它是 ab 的异位词。 起始索引等于 1 的子串是 ba, 它是 ab 的异位词。 起始索引等于 2 的子串是 ab, 它是 ab 的异位词。提示:1 s.length, p.length 3 * 104s和p仅包含小写字母1.动态窗口class Solution { public ListInteger findAnagrams(String s, String p) { if(p.length()s.length()){ return new ArrayListInteger(); } ListInteger ans new ArrayList(); int []sLennew int[26]; int []pLennew int[26]; for(int i0;ip.length();i){ sLen[s.charAt(i)-a]; pLen[p.charAt(i)-a]; } if(Arrays.equals(sLen,pLen)){ ans.add(0); } for(int i0;is.length()-p.length();i){ sLen[s.charAt(i)-a]--; sLen[s.charAt(ip.length())-a]; if(Arrays.equals(sLen,pLen)){ ans.add(i1); } } return ans; } }