写点什么

代码随想录训练营 Day02

作者:jjn0703
  • 2023-06-29
    江苏
  • 本文字数:1805 字

    阅读完需:约 6 分钟

相关概念

今天依然是数组相关的内容的学习。今日学到的几个点记录如下:

  • 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. 长度最小的子数组

 
复制代码

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)); } } }
复制代码


发布于: 刚刚阅读数: 4
用户头像

jjn0703

关注

Java工程师/终身学习者 2018-03-26 加入

USTC硕士/健身健美爱好者/Java工程师.

评论

发布
暂无评论
代码随想录训练营 Day02_算法_jjn0703_InfoQ写作社区