相关概念
今天依然是数组相关的内容的学习。今日学到的几个点记录如下:
- List 的 toArray 方法,打印数组用 Arrays.toString()方法
package jjn.carl.array; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author Jiang Jining * @since 2023-06-29 22:39 */ public class ToArrayDemo { public static void main(String[] args) { List<Integer> list = List.of(1, 2, 3, 4, 5); int[] converted = new int[list.size()]; for (int i = 0; i < list.size(); i++) { converted[i] = list.get(i); } System.out.println("Arrays.toString(array) = " + Arrays.toString(converted)); List<int[]> list2 = new ArrayList<>(); list2.add(new int[]{1, 2}); list2.add(new int[]{3, 4}); list2.add(new int[]{5, 6}); int[][] arr = list2.toArray(new int[0][]); for (int[] part : arr) { System.out.println("Arrays.toString(part) = " + Arrays.toString(part)); } } }
作业题
977. 有序数组的平方
public class LeetCode977_SortedSquares { public int[] sortedSquares(int[] nums) { // 双指针解法,倒序将结果放入数组 int left = 0, right = nums.length - 1; int[] result = new int[nums.length]; int start = right; // 结束条件, left == right while (left <= right) { // 按要求分别计算左边和右边的平方值 int leftSquare = nums[left] * nums[left]; int rightSquare = nums[right] * nums[right]; if (leftSquare >= rightSquare) { result[start] = leftSquare; left++; } else { result[start] = rightSquare; right--; } start--; } return result; } public static void main(String[] args) { int[] nums = new int[]{-4, -1, 0, 3, 10}; int[] result = new LeetCode977_SortedSquares().sortedSquares(nums); System.out.println("result = " + Arrays.toString(result)); } }
209. 长度最小的子数组
package jjn.carl.array; import java.util.Objects; /** * @author Jiang Jining * @since 2023-06-29 23:26 */ public class LeetCode209 { public int minSubArrayLen(int target, int[] nums) { if (nums.length == 0) { return 0; } int min = Integer.MAX_VALUE; int sum = 0; int left = 0, right = 0; while (right < nums.length) { sum += nums[right]; while (sum >= target) { min = Math.min(min, right - left + 1); sum = sum - nums[left]; left++; } right++; } return Objects.equals(min, Integer.MAX_VALUE) ? 0 : min; } public static void main(String[] args) { int target = 7; int[] nums = new int[]{2, 3, 1, 2, 4, 3}; int minSubArrayLen = new LeetCode209().minSubArrayLen(target, nums); System.out.println("minSubArrayLen = " + minSubArrayLen); } }
59. 螺旋矩阵 II
package jjn.carl.array; import java.util.Arrays; /** * @author Jiang Jining * @since 2023-06-29 23:01 */ public class LeetCode59 { // 按题目描述的方法,不断缩小边界 public int[][] generateMatrix(int n) { int[][] result = new int[n][n]; // left, right, top, bottom int left = 0, right = n - 1, top = 0, bottom = n - 1, current = 1; while (true) { for (int i = left; i <= right; i++, current++) { result[top][i] = current; } top++; if (top > bottom) { break; } for (int i = top; i <= bottom; i++, current++) { result[i][right] = current; } right--; if (right < left) { break; } for (int i = right; i >= left; i--, current++) { result[bottom][i] = current; } bottom--; if (bottom < top) { break; } for (int i = bottom; i >= top; i--, current++) { result[i][left] = current; } left++; if (left > right) { break; } } return result; } public static void main(String[] args) { int[][] generatedMatrix = new LeetCode59().generateMatrix(3); for (int[] matrix : generatedMatrix) { System.out.println("Arrays.toString(generatedMatrix[i]) = " + Arrays.toString(matrix)); } } }