public String minWindow(String s, String t){ Map<Character, Integer> needs = new HashMap<>(), window = new HashMap<>(); for(int i = 0; i < t.length(); i++){ needs.put(t.charAt(i), needs.getOrDefault(t.charAt(i), 0) + 1); } int left = 0, right = 0; int start = 0; // 最小覆盖子串的起始下标 int len = Integer.MAX_VALUE; // 最小覆盖子串的长度
publicintlengthOfLongestSubstring(String s){ Map<Character, Integer> window = new HashMap<>();
int left = 0, right = 0; int res = Integer.MIN_VALUE; while( right < s.length()){ char c = s.charAt(right); right++; window.put(c, window.getOrDefault(c, 0) + 1); while( window.get(c).compareTo(1) > 0){ // 有重复 需要缩小窗口 char l = s.charAt(left); window.put(l, window.get(l) - 1); left++; } res = Math.max(res, right - left); } return res == Integer.MIN_VALUE ? 0 : res; }
方法二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution{ publicintlengthOfLongestSubstring(String s){ int[] charIndex = newint[256]; int res = 0; int len = s.length(); for(int left = 0, right = 0; right < len; right++){ char c = s.charAt(right); left = Math.max(left, charIndex[c]); res = Math.max(res, right - left + 1);
publicint[][] findContinuousSequence(int target) { List<int[]> res = new ArrayList<>(); int len = (int)Math.ceil(target/2.0); for(int i = 1; i <= len;i++){ List<Integer> ans = new ArrayList<>(); int sum = 0; boolean flag = false; for(int j = i; j <= len; j++){ sum+= j; ans.add(j); if(sum == target){ flag = true; break; } } if(flag){ int[] a = ans.stream().mapToInt(Integer::valueOf).toArray(); res.add(a); } }
// 滑动窗口 publicint[][] findContinuousSequence(int target) { List<int[]> res = new ArrayList<>(); int l = 1; int r = 1; int sum = 0; while(r <= target/2+1){ // 求和 int[] a = newint[r-l+1]; for(int i = l ; i <= r; i++){ sum+=i; } if(sum > target){ l++; }elseif(sum < target){ r++; }else{ int k = 0; for(int i = l ; i <= r; i++){ a[k++] = i; } res.add(a); l++; } sum = 0; }