首页 > 其他分享 >Leetcode—矩阵置零

Leetcode—矩阵置零

时间:2023-12-22 13:11:36浏览次数:33  
标签:rows matrix int 矩阵 cols ++ length Leetcode

矩阵置零

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。

示例 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]]

方法1

  • 分析:根据题目所描述,我们可以先遍历数组找出所有0所在的位置,同时把这些位置对应标记在另一个数组temp中(即置这些位置上的值为1);在遍历temp数组,找出置为1的位置,将对应matrix数组的行和列置为0即可。
  • 方法1代码实现
    class Solution {
        public void setZeroes(int[][] matrix) {
            int rows = matrix.length;
            int cols = matrix[0].length;
            int[][] temp=new int[rows][cols];
            int flag=0;
            for(int i=0;i<rows;i++){
                for(int j=0;j<cols;j++){
                    if(matrix[i][j]==0){
                        temp[i][j]=1;
                    }
                }
            }
            for(int i=0;i<rows;i++){
                for(int j=0;j<cols;j++){
                    if(temp[i][j]==1){
                        for(int k=0;k<rows;k++){
                            matrix[k][j]=0;
                        }
                        for(int k=0;k<cols;k++){
                            matrix[i][k]=0;
                        }
                    }
                }
            }
        }
    }
    
  • 优化:可以将temp改成两个数组,分别记录每一行和每一列是否有零出现,可以减少内存空间的占用,使空间复杂度变为O(m+n)
    class Solution {
        public void setZeroes(int[][] matrix) {
            int rows = matrix.length;
            int cols = matrix[0].length;
            boolean[] row = new boolean[rows];
            boolean[] col = new boolean[cols];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    if (matrix[i][j] == 0) {
                        row[i] = col[j] = true;
                    }
                }
            }
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    if (row[i] || col[j]) {
                        matrix[i][j] = 0;
                    }
                }
            }
        }
    }
    

方法2

  • 分析:用矩阵的第一行和第一列代替方法一中的两个标记数组。因为我们不确定第一行和第一列本身有没有0,所有要先判断它们有没有0(即标记两个标记变量为true)。接着,循环判断用其他行与列,来标记第一行与第一列。然后反过来使用第一行与第一列去更新其他行与列,最后使用两个标记变量更新第一行与第一列即可。

  • 代码实现

    class Solution {
        public void setZeroes(int[][] matrix) {
            int rows = matrix.length;
            int cols = matrix[0].length;
            boolean flagCol0 = false, flagRow0 = false;
            for (int i = 0; i < rows; i++) {
                if (matrix[i][0] == 0) {
                    flagCol0 = true;
                }
            }
            for (int j = 0; j < cols; j++) {
                if (matrix[0][j] == 0) {
                    flagRow0 = true;
                }
            }
            for (int i = 1; i < rows; i++) {
                for (int j = 1; j < cols; j++) {
                    if (matrix[i][j] == 0) {
                        matrix[i][0] = matrix[0][j] = 0;
                    }
                }
            }
            for (int i = 1; i < rows; i++) {
                for (int j = 1; j < cols; j++) {
                    if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                        matrix[i][j] = 0;
                    }
                }
            }
            if (flagCol0) {
                for (int i = 0; i < rows; i++) {
                    matrix[i][0] = 0;
                }
            }
            if (flagRow0) {
                for (int j = 0; j < cols; j++) {
                    matrix[0][j] = 0;
                }
            }
        }
    }
    

标签:rows,matrix,int,矩阵,cols,++,length,Leetcode
From: https://www.cnblogs.com/qq286442936/p/17919712.html

相关文章

  • Leetcode 2521. 数组乘积中的不同质因数数目
    https://leetcode.cn/problems/distinct-prime-factors-of-product-of-array/description/给你一个正整数数组nums,对nums所有元素求积之后,找出并返回乘积中不同质因数的数目。注意:质数是指大于1且仅能被1及自身整除的数字。如果val2/val1是一个整数,则整数val......
  • 『LeetCode』2. 两数相加 Add Two Numbers
    『1』迭代法classSolution{//Iteration//Nisthesizeofl1,Misthesizeofl2//TimeComplexity:O(max(M,N))//SpaceComplexity:O(max(M,N))ifdummyiscountedelseO(1)publicListNodeaddTwoNumbers(ListNodel1,ListNodel2)......
  • Leetcode 2507. 使用质因数之和替换后可以取到的最小值 优化前 优化后
    https://leetcode.cn/problems/smallest-value-after-replacing-with-sum-of-prime-factors/description/给你一个正整数n。请你将n的值替换为n的质因数之和,重复这一过程。注意,如果n能够被某个质因数多次整除,则在求和时,应当包含这个质因数同样次数。返回n可以取到......
  • 『LeetCode』1. 两数之和 Two Sum
    『1』暴力法classSolution{//BruteForce//TimeComplexity:O(n^2)//SpaceComplexity:O(1)publicint[]twoSum(int[]nums,inttarget){for(inti=0;i<nums.length;i++){for(intj=i+1;j<nums.length......
  • 快速幂,快速乘,矩阵乘
    快速幂,快速乘,矩阵乘快速幂计算\(a^n(n\geqslant0)\),一般会对答案取个模例如计算\(5^{11}\),考虑11二进制\((1011)_2\)有\(5^{11}=5^8*5^2*5^1\)将n的二进制中为1的位置对应的a的\(2^k\)次幂相乘就能得到最终结果可以用\(O(\log{n})\)的时间复杂度计算a所有被用到的\(2^k......
  • [LeetCode Hot 100] LeetCode74. 搜索二维矩阵
    题目描述思路:二维矩阵坐标变换+二分查找二维矩阵坐标变换:只要知道二维数组的的行数m和列数n,二维数组的坐标(i,j)可以映射成一维的index=i*n+j;反过来也可以通过一维index反解出二维坐标i=index/n,j=index%n。(n是列数)把二维数组matrix的元素访问抽象成在......
  • [LeetCode] LeetCode852. 山脉数组的顶峰索引
    题目描述思路:用二分进行排除不满足条件的元素,最后剩下的元素即为答案往常我们使用「二分」进行查值,需要确保序列本身满足「二段性」:当选定一个端点(基准值)后,结合「一段满足&另一段不满足」的特性来实现“折半”的查找效果。但本题求的是峰顶索引值,如果我们选定数组头部或者尾......
  • [LeetCode] LeetCode162. 寻找峰值
    题目描述思路:同LeetCode852.山脉数组的顶峰索引注意:当nums数组只有一个元素的时候,这个元素就是顶元素因为根据题目:nums[-1]=nums[n]=-∞方法一:classSolution{publicintfindPeakElement(int[]nums){if(nums.length==1)return0;intl......
  • [LeetCode] 1362. Closest Divisors 最接近的因数
    Givenaninteger num,findtheclosesttwointegersinabsolutedifferencewhoseproductequals num+1 or num+2.Returnthetwointegersinanyorder.Example1:Input:num=8Output:[3,3]Explanation:Fornum+1=9,theclosestdivisorsare3&......
  • PCA(Principal Components Analysis)主成分分析: 一维列向量坐标的变换是左乘变换矩阵
    总结:一维列向量的坐标变换是左乘变换矩阵;一维行向量的坐标系基元变换是右乘变换矩阵;坐标变换坐标变换定义:把一个向量(或一个点)从一个高维(或3D)坐标系,转换到另一个高维(或3D)坐标系去。举个栗子:东北天坐标系上的点A坐标为(1,2,3),通过坐标变换到北西天坐标系,点A......