Set Matrix Zeroes leetcode java

题目

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

题解

这道题是CC150 1.7上面的原题。可以看上面详尽的解释,我就不写了。

代码如下:

1     public void setZeroes(int[][] matrix) {
 2         int m = matrix.length;
 3         int n = matrix[0].length;
 4         
 5         if(m==0||n==0)
 6             return;
 7         int[] flagr = new int[m];
 8         int[] flagc = new int[n];
 9         
10         for(int i=0;i<m;i++){
11             for(int j=0;j<n;j++){
12                 if(matrix[i][j]==0){
13                     flagr[i]= 1; 
14                     flagc[j]= 1;
15                 }
16             }
17         }
18         
19         for(int i=0;i<m;i++){
20             for(int j=0;j<n;j++){
21                 if(flagr[i]==1||flagc[j]==1){
22                     matrix[i][j]=0;
23                 }
24             }
25         }
26     }

Set Matrix Zeroes leetcode java,布布扣,bubuko.com

时间: 2025-01-02 14:45:39

Set Matrix Zeroes leetcode java的相关文章

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Set Matrix Zeroes leetcode

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Subscribe to see which companies asked this question 方法:用矩阵中的某一零行来记录零列的信息 过程如下: 1.从下往上遍历矩阵,找到第一个0元素,我们将其所在行作为记录行,用它来记录列号 2.接着从上向下遍历

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

【leetcode刷题笔记】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题解:因为题目要求原地算法,所以我们只能利用矩阵第一行和第一列存放置零信息. 首先遍历第一行和第一列,看他们是否需要全部置零,用两个变量first_column_zero和first_row_zero来记录: 遍历矩阵,如果某个位置matrix[i][j]出现了0,就把matrix[i][0]和Matrix[0

【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】

[070-Set Matrix Zeroes(矩阵置零)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题目大意 给定一个m*n的矩阵,如果某个位置是0.将对应的行和列设置为0. 解题思路 先对矩阵进行扫描,标记要进行置0的行和列,对要进行置0的行在第0列上进行标记,对置0的列在

LeetCode: Set Matrix Zeroes [073]

[题目] Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. [题意] 给定一个mXn的矩阵,如果其中的元素为0,则对应的行和列都用0填充. 不能申请额外的空间. [思路] 第一行和第一列空出来标记需要置0的列和行 第一遍扫描: 扫描第一行,判断第一行是否需要清零 扫描第一列,判断第一列是否需要清零 扫描[1,1]~[m-1, n-1]的区块,如果某个位置上的值

【Leetcode】Set Matrix Zeroes

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)