数组篇
两数之和,二分查找,移除元素,长度最小子数组,螺旋矩阵
数组
1.两数之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public int[] twoSum(int[] nums, int target) { int n = nums.length; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (nums[i] + nums[j] == target) return new int[] { i, j }; } } return new int[0]; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (map.containsKey(target - nums[i])) { int j = map.get(target - nums[i]); return new int[] { i, j }; } map.put(nums[i], i); } return new int[0]; } }
|
二分查找
704.二分查找
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } else { return mid; } } return -1; } }
|
35.搜索插入位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int searchInsert(int[] nums, int target) { int left = 0; int right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } else { return mid; } } return left; } }
|
34. 在排序数组中查找元素的第一个和最后一个位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| class Solution { public int[] searchRange(int[] nums, int target) { int len = nums.length; int left = -1; int right = -1; for (int i = 0; i < len; i++) { if (nums[i] == target) { left = i; break; } } for (int i = len-1; i >= 0; i--) { if (nums[i] == target) { right = i; break; } } return new int[] { left, right }; } }
class Solution { public int[] searchRange(int[] nums, int target) { int first = -1; int end = -1; int left = 0; int right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } else { first = mid; right = mid - 1; } }
left = 0; right = nums.length - 1; while (left <= right) { int mid = left + ((right - left) >> 1); if (nums[mid] > target) { right = mid - 1; } else if (nums[mid] < target) { left = mid + 1; } else { end = mid; left = mid + 1; } } return new int[] { first, end }; } }
|
69. x的平方根
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int mySqrt(int x) { int left = 0; int right = x; int ans = -1; while (left <= right) { int mid = left + ((right - left) >> 1); if ((long) mid * mid > x) { right = mid - 1; } else if ((long) mid * mid < x) { ans = mid; left = mid + 1; } else { return mid; } } return ans; } }
|
367.有效的完全平方数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public boolean isPerfectSquare(int num) { int left = 0; int right = num; while (left <= right) { int mid = left + ((right - left) >> 1); if ((long) mid * mid > num) { right = mid - 1; } else if ((long) mid * mid < num) { left = mid + 1; } else { return true; } } return false; } }
|
移除元素
27.移除元素
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public int removeElement(int[] nums, int val) { int slow = 0; for (int fast = 0; fast < nums.length; fast++) { if (nums[fast] != val) { nums[slow++] = nums[fast]; } } return slow; } }
|
26.删除有序数组中的重复项
1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public int removeDuplicates(int[] nums) { int slow = 1; for (int fast = 1; fast < nums.length ; fast++) { if (nums[fast] != nums[fast - 1]) { nums[slow++] = nums[fast]; } } return slow; } }
|
283.移动0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Solution { public void moveZeroes(int[] nums) { int slow = 0; for (int fast = 0; fast < nums.length; fast++) { if (nums[fast] != 0) { swap(nums, fast, slow); slow++; } } }
public void swap(int nums[], int a, int b) { int t = nums[a]; nums[a] = nums[b]; nums[b] = t; } }
class Solution { public void moveZeroes(int[] nums) { int slow = 0; for (int fast = 0; fast < nums.length; fast++) { if (nums[fast] != 0) { nums[slow++] = nums[fast]; } } for (int i = slow; i < nums.length; i++) { nums[i] = 0; } } }
|
844.比较含退格的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| class Solution { public boolean backspaceCompare(String s, String t) { StringBuilder s1 = new StringBuilder(); StringBuilder t1 = new StringBuilder(); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c != '#') { s1.append(c); } else if (s1.length() != 0) { s1.deleteCharAt(s1.length() - 1); } } for (int i = 0; i < t.length(); i++) { char c = t.charAt(i); if (c != '#') { t1.append(c); } else if (t1.length() != 0) { t1.deleteCharAt(t1.length() - 1); } } return s1.toString().equals(t1.toString()); } }
class Solution { public boolean backspaceCompare(String s, String t) { Stack<Character> stacks=new Stack(); Stack<Character> stackt=new Stack(); for (int i = 0; i < s.length(); i++) { if(s.charAt(i)!='#'){ stacks.push(s.charAt(i)); }else{ if(!stacks.isEmpty()){ stacks.pop(); } } } for (int i = 0; i < t.length(); i++) { if(t.charAt(i)!='#'){ stackt.push(t.charAt(i)); }else{ if(!stackt.isEmpty()){ stackt.pop(); } } } return stacks.equals(stackt); } }
class Solution { public boolean backspaceCompare(String s, String t) { int i = s.length() - 1; int j = t.length() - 1; int countS = 0; int countT = 0;
while (i >= 0 || j >= 0) { while (i >= 0) { char c = s.charAt(i); if (c == '#') { countS++; i--; } else if (c != '#' && countS != 0) { countS--; i--; } else { break; } } while (j >= 0) { char c = t.charAt(j); if (c == '#') { countT++; j--; } else if (c != '#' && countT != 0) { countT--; j--; } else { break; } } if (i >= 0 && j >= 0) { if (s.charAt(i) != t.charAt(j)) { return false; } } else if (i < 0 && j < 0) { return true; } else { return false; } i--; j--; } return true; } }
|
977.有序数组的平方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| class Solution { public int[] sortedSquares(int[] nums) { int[] ans = new int[nums.length]; for (int i = 0; i < nums.length; i++) { ans[i] = nums[i] * nums[i]; } Arrays.sort(ans); return ans; } }
class Solution { public int[] sortedSquares(int[] nums) { int len = nums.length; int ans[] = new int[len]; int i = 0; int j = len - 1; int k = len - 1; while (i <= j) { if (nums[i] * nums[i] >= nums[j] * nums[j]) { ans[k--] = nums[i] * nums[i]; i++; } else { ans[k--] = nums[j] * nums[j]; j--; } } return ans; } }
|
长度最小的子数组
209.长度最小的子数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| class Solution { public int minSubArrayLen(int target, int[] nums) { int ans = Integer.MAX_VALUE; int l = nums.length; int substring = 0; for (int i = 0; i < l; i++) { int sum = 0; for (int j = i; j < l; j++) { sum += nums[j]; if (sum >= target) { substring = j - i + 1; ans = Math.min(ans, substring); break; } } } return ans == Integer.MAX_VALUE ? 0 : ans; } }
class Solution { public int minSubArrayLen(int target, int[] nums) { int ans = Integer.MAX_VALUE; int l = nums.length; int substring = 0; int i = 0; int sum = 0; for (int j = 0; j < l; j++) { sum += nums[j]; while (sum >= target) { substring = j - i + 1; ans = Math.min(ans, substring); sum -= nums[i]; i++; } } return ans == Integer.MAX_VALUE ? 0 : ans; } }
|
904.水果成篮
(找最多包含两种元素的最长字串,并且返回其长度。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { public int totalFruit(int[] fruits) { int len = fruits.length; if (len <= 2) return len; HashMap<Integer, Integer> map = new HashMap<>(); int left = 0; int ans = 0; for (int right = 0; right < len; right++) { int a = fruits[right]; map.put(a, map.getOrDefault(a, 0) + 1); while (map.size() > 2) { int b = fruits[left]; map.put(b, map.get(b) - 1); if (map.get(b) == 0) { map.remove(b); } left++; } ans = Math.max(ans, right - left + 1); } return ans; } }
|
76.覆盖最小字串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| class Solution { public String minWindow(String s, String t) { Map<Character,Integer> window = new HashMap(); Map<Character,Integer> need = new HashMap(); for (int i = 0; i < t.length(); i++) { char needChar = t.charAt(i); need.put(needChar,need.getOrDefault(needChar,0)+1); }
int left = 0,right = 0,valid = 0; int len = Integer.MAX_VALUE; int start = 0; while(right < s.length()){ char addChar = s.charAt(right); window.put(addChar,window.getOrDefault(addChar,0) + 1); right++; if(need.containsKey(addChar) && window.get(addChar).equals(need.get(addChar))){ valid++; } while(valid == need.size()){ if(right - left < len){ len = right - left ; start = left; } char removeChar = s.charAt(left); if(need.containsKey(removeChar) && window.get(removeChar).equals(need.get(removeChar))){ valid--; } window.put(removeChar,window.get(removeChar) - 1); left++; }
} return len == Integer.MAX_VALUE?"":s.substring(start,start+len); } }
|
螺旋矩阵
59.螺旋矩阵II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Solution { public int[][] generateMatrix(int n) { int ans[][] = new int[n][n]; int loop = 0; int i, j; int count = 1; int start = 0; while ((loop++) < n / 2) { for (j = start; j < n - start - 1; j++) { ans[start][j] = count++; } for (i = start; i < n - start - 1; i++) { ans[i][j] = count++; } for (; j > start; j--) { ans[i][j] = count++; } for (; i > start; i--) { ans[i][j] = count++; } start++; } if (n % 2 == 1) { ans[start][start] = count; } return ans; } }
|
54.螺旋矩阵
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| class Solution { public List<Integer> spiralOrder(int[][] matrix) { int m = matrix.length; int n = matrix[0].length; int l = Math.min(m, n); int loop = 0; List<Integer> list = new ArrayList<>(); int i, j; int startx = 0; int starty = 0; while ((loop++) < l / 2) { for (j = startx; j < n - startx - 1; j++) { list.add(matrix[startx][j]); } for (i = starty; i < m - starty - 1; i++) { list.add(matrix[i][j]); } for (; j > startx; j--) { list.add(matrix[i][j]); } for (; i > starty; i--) { list.add(matrix[i][j]); } startx++; starty++; } if (l % 2 == 1) { if (m < n) { for (j = startx; j < n - startx; j++) { list.add(matrix[startx][j]); } } else { for (i = starty; i < m - starty; i++) { list.add(matrix[i][starty]); } } } return list; } }
|
LCR 146. 螺旋遍历二维数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| class Solution { public int[] spiralArray(int[][] matrix) { int m = matrix.length; if (m == 0) { return new int[0]; } int n = matrix[0].length; int ans[] = new int[m * n]; int count = 0; int l = Math.min(m, n); int loop = 0; int i, j; int startx = 0; int starty = 0; while ((loop++) < l / 2) { for (j = startx; j < n - startx - 1; j++) { ans[count++] = matrix[startx][j]; } for (i = starty; i < m - starty - 1; i++) { ans[count++] = matrix[i][j]; } for (; j > startx; j--) { ans[count++] = matrix[i][j]; } for (; i > starty; i--) { ans[count++] = matrix[i][j]; } startx++; starty++; } if (l % 2 == 1) { if (m < n) { for (j = startx; j < n - startx; j++) { ans[count++] = matrix[startx][j]; } } else { for (i = starty; i < m - starty; i++) { ans[count++] = matrix[i][starty]; } } } return ans;
} }
|