[LeetCode] Set Matrix Zeros

 1 public class Solution {
 2     public void setZeroes(int[][] matrix) {
 3         int m=matrix.length;
 4         int n=matrix[0].length;
 5         boolean hasZeroCol = false, hasZeroRow = false;
 6
 7         for (int i=0; i<m; i++)
 8             if (matrix[i][0] == 0) { hasZeroCol = true; break; }
 9
10         for (int j=0; j<n; j++)
11             if (matrix[0][j] == 0) { hasZeroRow = true; break; }
12
13         for (int i=1; i<m; i++) {
14             for (int j=1; j<n; j++) {
15                 if (matrix[i][j] == 0) {
16                     matrix[0][j] = 0;
17                     matrix[i][0] = 0;
18                 }
19             }
20         }
21
22         for (int i=1; i<m; i++) {
23             for (int j=1; j<n; j++) {
24                 if (matrix[i][0] == 0 || matrix[0][j] == 0)
25                     matrix[i][j] = 0;
26             }
27         }
28
29         if (hasZeroCol)
30             for (int i=0; i<m; i++) matrix[i][0] = 0;
31
32         if (hasZeroRow)
33             for (int i=0; i<n; i++) matrix[0][i] = 0;
34
35
36     }
37 }
时间: 2024-10-06 17:06:24

[LeetCode] Set Matrix Zeros的相关文章

LeetCode:Spiral Matrix I II

Spiral Matrix 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]. 打印螺旋矩阵 逐个

LeetCode: Spiral Matrix [058]

[题目] 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]. [题意] 螺旋输出MxN矩阵 [注意

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: Spiral Matrix II [058]

[题目] 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 ] ] [题意] 给定整数n, 将1,2,3...nxn个数按螺旋旋转的方式填入nxn的矩

LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

Spiral Matrix IIGiven 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 ]] SOLUTION 1: 还是与上一题Spiral Matrix类似

LeetCode 73. Set Matrix Zeros(矩阵赋零)

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

【leetcode】Set Matrix Zeros

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(

[LintCode] Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Example Given a matrix [ [1,2], [0,3] ], return[[0,2],[0,0]] Challenge Did you use extra space?A straight forward solution using O(mn) space is probably a b

[Leetcode] 01 Matrix

问题: https://leetcode.com/problems/01-matrix/#/description 基本思路:广度优先遍历,根据所有最短距离为N的格找到所有距离为N+1的格,直到所有的格都找到了最短距离. 具体步骤: 首先初始化矩阵,值为0的格其最短距离也是0,保持不变:值为1的格是后面需要计算最短距离的格,我们需要一个整数来标识它们,这里可以选择一个负数或者整数的最大值. 然后进行若干轮计算,第N轮会根据已经计算出的最短距离为N-1的所有格,来找出所有最短距离为N的格. 例如,