代码随想录
LeetCode 题解
数组问题最常见
滑动窗口:窗口大小固定还是不固定,什么时候应该扩展窗口
1、二分查找
704 - 二分查找
更多二分查找
| public int search(int[] nums, int target) { |
| int l = 0; |
| int r = nums.length - 1; |
| int mid; |
| |
| while (l <= r) { |
| mid = l + (r - l) / 2; |
| if (nums[mid] == target) return mid; |
| if (nums[mid] > target) r = mid - 1; |
| else l = mid + 1; |
| } |
| |
| return -1; |
| } |
2、移除元素
27 - 移除元素
| public int removeElement(int[] nums, int val) { |
| |
| int end = nums.length; |
| int i = 0; |
| while (i < end) { |
| if (nums[i] == val) swap(nums, i, --end); |
| else i++; |
| } |
| return end; |
| } |
| |
| private void swap(int[] nums, int a, int b) { |
| int k = nums[a]; |
| nums[a] = nums[b]; |
| nums[b] = k; |
| } |
3、有序数组的平方
977 - 有序数组的平方
| public int[] sortedSquares(int[] nums) { |
| int[] arr = new int[nums.length]; |
| int i = arr.length - 1; |
| |
| |
| int p1 = 0; |
| int p2 = nums.length - 1; |
| while (p1 <= p2) { |
| if (Math.abs(nums[p1]) > Math.abs(nums[p2])) { |
| arr[i--] = nums[p1] * nums[p1]; |
| p1++; |
| } else { |
| arr[i--] = nums[p2] * nums[p2]; |
| p2--; |
| } |
| } |
| |
| return arr; |
| } |
4、长度最小的子数组
209 - 长度最小的子数组
滑动窗口
窗口大小不固定,和小于目标的时候应该扩展窗口
| public int minSubArrayLen(int target, int[] nums) { |
| int l = 0, r = -1; |
| int sum = 0; |
| int res = nums.length + 1; |
| |
| |
| while (l < nums.length) { |
| if (r + 1 < nums.length && sum < target) sum += nums[++r]; |
| else sum -= nums[l++]; |
| |
| if (sum >= target) res = Math.min(res, r - l + 1); |
| } |
| |
| return res != nums.length + 1 ? res : 0; |
| } |
5、螺旋矩阵 II
59 - 螺旋矩阵 II
| |
| |
| |
| |
| public int[][] generateMatrix(int n) { |
| int[][] arr = new int[n][n]; |
| int num = 1; |
| int left = 0, right = n - 1; |
| int top = 0, bottom = n - 1; |
| |
| while (num <= n * n) { |
| for (int i = left; i <= right; i++) arr[top][i] = num++; |
| top++; |
| |
| for (int i = top; i <= bottom; i++) arr[i][right] = num++; |
| right--; |
| |
| for (int i = right; i >= left; i--) arr[bottom][i] = num++; |
| bottom--; |
| |
| for (int i = bottom; i >= top; i--) arr[i][left] = num++; |
| left++; |
| } |
| |
| return arr; |
| } |
6、更多题目
3 - 无重复字符的最长子串
窗口大小不固定,当前无重复字符的时候应该扩展窗口
| public int lengthOfLongestSubstring(String s) { |
| int l = 0, r = -1; |
| int[] freq = new int[256]; |
| int res = 0; |
| |
| |
| while (l < s.length()) { |
| if (r + 1 < s.length() && freq[s.charAt(r + 1)] == 0) freq[s.charAt(++r)]++; |
| else freq[s.charAt(l++)]--; |
| |
| res = Math.max(res, r - l + 1); |
| } |
| |
| return res; |
| } |
438 - 找到字符串中所有字母异位词
窗口大小固定
| public List<Integer> findAnagrams(String s, String p) { |
| List<Integer> res = new LinkedList<>(); |
| if (s.length() < p.length()) return res; |
| |
| int[] freq_p = new int[26]; |
| for (char c : p.toCharArray()) freq_p[c - 'a']++; |
| |
| int l = 0, r = p.length() - 1; |
| int[] freq_s = new int[26]; |
| |
| for (int i = 0; i < p.length(); i++) freq_s[s.charAt(i) - 'a']++; |
| if (same(freq_p, freq_s)) res.add(0); |
| |
| while (true) { |
| if (r == s.length() - 1) break; |
| |
| r++; |
| freq_s[s.charAt(r) - 'a']++; |
| freq_s[s.charAt(l) - 'a']--; |
| l++; |
| if (same(freq_p, freq_s)) res.add(l); |
| } |
| |
| return res; |
| } |
| |
| private boolean same(int[] freq_p, int[] freq_s) { |
| for (int i = 0; i < 26; i++) { |
| if (freq_p[i] != freq_s[i]) return false; |
| } |
| return true; |
| } |
76 - 最小覆盖子串
窗口大小不固定,还没覆盖完目标的时候应该扩展窗口
| public String minWindow(String s, String t) { |
| if (s.length() < t.length()) return ""; |
| |
| |
| int[] freq_t = new int[256]; |
| for (char c : t.toCharArray()) freq_t[c]++; |
| |
| |
| int l = 0, r = -1; |
| int[] freq_s = new int[256]; |
| int count = 0; |
| |
| int startIndex = -1; |
| int minLength = s.length() + 1; |
| |
| while (l < s.length()) { |
| if (r + 1 < s.length() && count < t.length()) { |
| r++; |
| char c = s.charAt(r); |
| freq_s[c]++; |
| if (freq_s[c] <= freq_t[c]) count++; |
| } else { |
| char c = s.charAt(l); |
| l++; |
| freq_s[c]--; |
| if (freq_s[c] < freq_t[c]) count--; |
| } |
| |
| if (count == t.length() && r - l + 1 < minLength) { |
| startIndex = l; |
| minLength = r - l + 1; |
| } |
| } |
| |
| if (startIndex == -1) return ""; |
| return s.substring(startIndex, startIndex + minLength); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步