3.13 矩阵

This commit is contained in:
zhangsan 2025-03-13 18:56:26 +08:00
parent a0c7134799
commit b0caeef468
10 changed files with 333 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}
}
}

View File

@ -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<row && j>=0){
if(target<matrix[i][j])
j-=1;
else if(target==matrix[i][j])
return true;
else
i+=1;
}
return false;
}
}

View File

@ -0,0 +1,43 @@
package matrix;
import java.util.HashSet;
/**
* 题目73. 矩阵置零 (setZeroes)
* 描述给定一个 m x n 的矩阵如果一个元素为 0 则将其所在行和列的所有元素都设为 0 请使用 原地 算法
* 链接https://leetcode.cn/problems/set-matrix-zeroes/
示例 1:
输入matrix = [[1,1,1],[1,0,1],[1,1,1]]
输出[[1,0,1],[0,0,0],[1,0,1]]
示例 2:
输入matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
输出[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
*/
public class SetZeroes {
public void setZeroes(int[][] matrix) {
int row=matrix.length;
int col=matrix[0].length;
HashSet<Integer>iset=new HashSet<>();
HashSet<Integer>jset=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;
}
}
}
}

View File

@ -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<Integer> spiralOrder(int[][] matrix) {
List<Integer> 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;
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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");
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}