73. Set Matrix Zeroes java solutions

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?

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public void setZeroes(int[][] matrix) {
 3         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
 4         int m = matrix.length, n = matrix[0].length;
 5         int[] rowflag = new int[m];
 6         int[] colflag = new int[n];
 7         for(int i = 0; i < m; i++){
 8             for(int j = 0; j < n; j++){
 9                 if(matrix[i][j] == 0){
10                     rowflag[i] = 1;
11                     colflag[j] = 1;
12                 }
13             }
14         }
15         for(int i = 0; i < m; i++){
16             if(rowflag[i] == 1){
17                 for(int j = 0; j < n; j++){
18                     matrix[i][j] = 0;
19                 }
20             }
21         }
22         for(int i = 0; i < n; i++){
23             if(colflag[i] == 1){
24                 for(int j = 0; j < m; j++){
25                     matrix[j][i] = 0;
26                 }
27             }
28         }
29     }
30 }
时间: 2024-10-11 07:05:35

73. Set Matrix Zeroes java solutions的相关文章

leetcode 73 Set Matrix Zeroes ----- 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

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

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. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

73. Set Matrix Zeroes(js)

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. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: [   [0,1,2,0],   [

【LeetCode】Set Matrix Zeroes (2 solutions)

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. 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

[LeetCode#73]Set Matrix Zeroes

The problem: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. My analysis: This is a very typical quesition in metricing our understanding about matrix. The main idea is that : We could not base on the cha

[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 improvement uses O(m + n) space, but still

73. Set Matrix Zeroes? (Graph)

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 improvement uses O(m + n) space, but stil

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 improvement uses O(m + n) space, but

[leedcode 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. public class Solution { public void setZeroes(int[][] matrix) { //题目要求不使用额外空间,因此需要借助矩阵本身的空间来辅助存储, //这里借用了矩阵的第一行和第一列来辅助纪录该行或该列是否为0.由于第一行第一列自身发生了改变,再用两个变量记录第一