From b0caeef4684ab9a2c14df3bb205973e85ab94b3f Mon Sep 17 00:00:00 2001 From: zhangsan <646228430@qq.com> Date: Thu, 13 Mar 2025 18:56:26 +0800 Subject: [PATCH] =?UTF-8?q?3.13=20=E7=9F=A9=E9=98=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/array/ProductExceptSelf.java | 39 +++++++++++ src/main/java/matrix/RotateImage.java | 54 +++++++++++++++ src/main/java/matrix/SearchMatrix.java | 34 ++++++++++ src/main/java/matrix/SetZeroes.java | 43 ++++++++++++ src/main/java/matrix/SpiralOrder.java | 66 +++++++++++++++++++ .../java/array/ProductExceptSelfTest.java | 18 +++++ src/test/java/matrix/RotateImageTest.java | 19 ++++++ src/test/java/matrix/SearchMatrixTest.java | 20 ++++++ src/test/java/matrix/SetZeroesTest.java | 18 +++++ src/test/java/matrix/SpiralOrderTest.java | 22 +++++++ 10 files changed, 333 insertions(+) create mode 100644 src/main/java/array/ProductExceptSelf.java create mode 100644 src/main/java/matrix/RotateImage.java create mode 100644 src/main/java/matrix/SearchMatrix.java create mode 100644 src/main/java/matrix/SetZeroes.java create mode 100644 src/main/java/matrix/SpiralOrder.java create mode 100644 src/test/java/array/ProductExceptSelfTest.java create mode 100644 src/test/java/matrix/RotateImageTest.java create mode 100644 src/test/java/matrix/SearchMatrixTest.java create mode 100644 src/test/java/matrix/SetZeroesTest.java create mode 100644 src/test/java/matrix/SpiralOrderTest.java diff --git a/src/main/java/array/ProductExceptSelf.java b/src/main/java/array/ProductExceptSelf.java new file mode 100644 index 0000000..e5289a9 --- /dev/null +++ b/src/main/java/array/ProductExceptSelf.java @@ -0,0 +1,39 @@ +package array; +/** + * 题目:238. 除自身以外数组的乘积 (productExceptSelf) + * 描述:给你一个整数数组 nums,返回 数组 answer ,其中 answer[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积 。 + * 题目数据 保证 数组 nums之中任意元素的全部前缀元素和后缀的乘积都在 32 位 整数范围内。 + * 请 不要使用除法,且在 O(n) 时间复杂度内完成此题。 + + * 链接:https://leetcode.cn/problems/product-of-array-except-self/ + * + 示例 1: + 输入: nums = [1,2,3,4] + 输出: [24,12,8,6] + + 示例 2: + 输入: nums = [-1,1,0,-3,3] + 输出: [0,0,9,0,0] + */ +//没做出来 +public class ProductExceptSelf { + public int[] productExceptSelf(int[] nums) { + int size=nums.length; + if(size==0) + return new int[]{}; + int[] left=new int[size],right=new int[size]; + left[0]=1; + right[size-1]=1; + for (int i = 1; i < size; i++) { + left[i]=left[i-1]*nums[i-1]; + } + for (int j = size-2; j >=0 ; j--) { + right[j]=right[j+1]*nums[j+1]; + } + int[]res=new int[size]; + for (int k = 0; k < size; k++) { + res[k]=left[k]*right[k]; + } + return res; + } +} diff --git a/src/main/java/matrix/RotateImage.java b/src/main/java/matrix/RotateImage.java new file mode 100644 index 0000000..0e289a1 --- /dev/null +++ b/src/main/java/matrix/RotateImage.java @@ -0,0 +1,54 @@ +package matrix; +/** + * 题目:48. 旋转图像 (setZeroes) + * 描述:给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 + * 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 + + * 链接:https://leetcode.cn/problems/rotate-image/description/ + * + 示例 1: + 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] + 输出:[[7,4,1],[8,5,2],[9,6,3]] + + 示例 2: + 输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] + 输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] + */ +//不会原地旋转,只会借助辅助数组 +public class RotateImage { + public void rotate(int[][] matrix) { + int n = matrix.length; + int[][] matrix_new = new int[n][n]; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + matrix_new[j][n - i - 1] = matrix[i][j]; + } + } + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + matrix[i][j] = matrix_new[i][j]; + } + } + } + //原地旋转, (i,j)->(j,n-i-1) == 先翻转(j,i)->再交换左右列(j,n-i-1) + public void rotate1(int[][] matrix) { + int n = matrix.length; + // 1. 转置矩阵:行列互换 + for (int i = 0; i < n; i++) { + // j 从 i+1 开始,避免重复交换 + for (int j = i + 1; j < n; j++) { + int temp = matrix[i][j]; + matrix[i][j] = matrix[j][i]; + matrix[j][i] = temp; + } + } + // 2. 翻转每一行:相当于水平翻转 + for (int i = 0; i < n; i++) { + for (int j = 0; j < n / 2; j++) { + int temp = matrix[i][j]; + matrix[i][j] = matrix[i][n - 1 - j]; + matrix[i][n - 1 - j] = temp; + } + } + } +} diff --git a/src/main/java/matrix/SearchMatrix.java b/src/main/java/matrix/SearchMatrix.java new file mode 100644 index 0000000..fc2508f --- /dev/null +++ b/src/main/java/matrix/SearchMatrix.java @@ -0,0 +1,34 @@ +package matrix; +/** + * 题目:240. 搜索二维矩阵 II (searchMatrix) + * 描述:编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性: + 每行的元素从左到右升序排列。 + 每列的元素从上到下升序排列。 + + * 链接:https://leetcode.cn/problems/search-a-2d-matrix-ii/ + * + 示例 1: + 输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5 + 输出:true + + 示例 2: + 输入:matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20 + 输出:false + */ +//高效的方法不会 +public class SearchMatrix { + //矩阵逆时针旋转45,右上角的元素看作树根,左边比他小,右边比他大 + public boolean searchMatrix(int[][] matrix, int target) { + int row=matrix.length,col=matrix[0].length; + int i=0,j=col-1; + while (i=0){ + if(targetiset=new HashSet<>(); + HashSetjset=new HashSet<>(); + for (int i = 0; i < row; i++) { + for (int j = 0; j < col; j++) { + if(matrix[i][j]==0) { + iset.add(i); + jset.add(j); + } + } + } + for (Integer integer : iset) { + for (int j = 0; j < col; j++) { + matrix[integer][j]=0; + } + } + for (Integer integer : jset) { + for (int i = 0; i < row; i++) { + matrix[i][integer]=0; + } + } + } +} diff --git a/src/main/java/matrix/SpiralOrder.java b/src/main/java/matrix/SpiralOrder.java new file mode 100644 index 0000000..e146c04 --- /dev/null +++ b/src/main/java/matrix/SpiralOrder.java @@ -0,0 +1,66 @@ +package matrix; + +import java.util.ArrayList; +import java.util.List; +/** + * 题目:54. 螺旋矩阵 (spiralOrder) + * 描述:给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。 + + * 链接:https://leetcode.cn/problems/spiral-matrix/ + 示例 1: + 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]] + 输出:[1,2,3,6,9,8,7,4,5] + + 示例 2: + 输入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] + 输出:[1,2,3,4,8,12,11,10,9,5,6,7] + */ +//没想出 +public class SpiralOrder { + public List spiralOrder(int[][] matrix) { + List result = new ArrayList<>(); + // 边界判断:空矩阵直接返回空结果 + if (matrix == null || matrix.length == 0) { + return result; + } + + // 初始化边界变量 + int top = 0; // 上边界(第一行索引) + int bottom = matrix.length - 1; // 下边界(最后一行索引) + int left = 0; // 左边界(第一列索引) + int right = matrix[0].length - 1; // 右边界(最后一列索引) + + // 当上边界 <= 下边界 且 左边界 <= 右边界 时继续遍历 + while (top <= bottom && left <= right) { + // 1. 从左到右遍历上边界 + for (int j = left; j <= right; j++) { + result.add(matrix[top][j]); + } + top++; // 遍历完上边界后,上边界下移 + + // 2. 从上到下遍历右边界 + for (int i = top; i <= bottom; i++) { + result.add(matrix[i][right]); + } + right--; // 遍历完右边界后,右边界左移 + + // 3. 判断是否还有剩余的行,若有,则从右到左遍历下边界 + if (top <= bottom) { + for (int j = right; j >= left; j--) { + result.add(matrix[bottom][j]); + } + bottom--; // 遍历完下边界后,下边界上移 + } + + // 4. 判断是否还有剩余的列,若有,则从下到上遍历左边界 + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result.add(matrix[i][left]); + } + left++; // 遍历完左边界后,左边界右移 + } + } + + return result; + } +} diff --git a/src/test/java/array/ProductExceptSelfTest.java b/src/test/java/array/ProductExceptSelfTest.java new file mode 100644 index 0000000..1a6f6a9 --- /dev/null +++ b/src/test/java/array/ProductExceptSelfTest.java @@ -0,0 +1,18 @@ +package array; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class ProductExceptSelfTest { + + @Test + public void productExceptSelf() { + int[]nums=new int[]{-1,1,0,-3,3}; + ProductExceptSelf solution = new ProductExceptSelf(); + int[]res=solution.productExceptSelf(nums); + System.out.println(Arrays.toString(res)); + } +} \ No newline at end of file diff --git a/src/test/java/matrix/RotateImageTest.java b/src/test/java/matrix/RotateImageTest.java new file mode 100644 index 0000000..6f16428 --- /dev/null +++ b/src/test/java/matrix/RotateImageTest.java @@ -0,0 +1,19 @@ +package matrix; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + + +public class RotateImageTest { + + @Test + public void rotate() { + RotateImage solution = new RotateImage(); + int[][] nums=new int[][]{{1,2,3},{4,5,6},{7,8,9}}; + solution.rotate(nums); + System.out.println(Arrays.deepToString(nums)); + } +} \ No newline at end of file diff --git a/src/test/java/matrix/SearchMatrixTest.java b/src/test/java/matrix/SearchMatrixTest.java new file mode 100644 index 0000000..47b58e9 --- /dev/null +++ b/src/test/java/matrix/SearchMatrixTest.java @@ -0,0 +1,20 @@ +package matrix; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SearchMatrixTest { + + @Test + public void searchMatrix() { + int [][]martix={{1,4,7,11,15},{2,5,8,12,19},{3,6,9,16,22},{10,13,14,17,24},{18,21,23,26,30}}; + int targer=20; + SearchMatrix solution = new SearchMatrix(); + boolean res=solution.searchMatrix(martix,targer); + if(res) + System.out.println("yes"); + else + System.out.println("no"); + } +} \ No newline at end of file diff --git a/src/test/java/matrix/SetZeroesTest.java b/src/test/java/matrix/SetZeroesTest.java new file mode 100644 index 0000000..60b3c41 --- /dev/null +++ b/src/test/java/matrix/SetZeroesTest.java @@ -0,0 +1,18 @@ +package matrix; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class SetZeroesTest { + + @Test + public void setZeroes() { + int[][]matrix=new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}}; + SetZeroes solution = new SetZeroes(); + solution.setZeroes(matrix); + System.out.println(Arrays.deepToString(matrix)); + } +} \ No newline at end of file diff --git a/src/test/java/matrix/SpiralOrderTest.java b/src/test/java/matrix/SpiralOrderTest.java new file mode 100644 index 0000000..e0be4d1 --- /dev/null +++ b/src/test/java/matrix/SpiralOrderTest.java @@ -0,0 +1,22 @@ +package matrix; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.*; + +public class SpiralOrderTest { + + @Test + public void spiralOrder() { + int[][] matrix = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + SpiralOrder solution = new SpiralOrder(); + solution.spiralOrder(matrix); + System.out.println(Arrays.deepToString(matrix)); + } +} \ No newline at end of file