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 not the best solution. Could you devise a constant space solution?

代码:

题目很简单,就是矩阵中如果存在值为0的元素,就把该元素对应的行和列上面的元素全都改成0。

思考了半天,还是遍历了两遍,复杂度至少在O(mn):

public void setZeroes(int[][] matrix) {
         int i,j;
         ArrayList<Integer> row = new ArrayList<Integer>();
         ArrayList<Integer> col = new ArrayList<Integer>();        
         //第一个循环,遍历一遍,记录哪些行和哪些列需要改成0
         for (i=0;i < matrix.length;i++)
         { 
          for (j=0;j<matrix[i].length;j++)
          {
           if(matrix[i][j] == 0)
           {
            row.add(i);
            col.add(j);
           }
          }       
         }
       //第二个循环,又遍历一遍,根据需要变为0的行和列修改对应元素
         for (i=0;i < matrix.length;i++)
         { 
          for (j=0;j<matrix[i].length;j++)
          {
           if(row.contains(i)||col.contains(j))
           {
            matrix[i][j] =0;
//            System.out.print("修改第"+i+"行,第"+j+"列");
           } 
          }       
         }
 
     }

题目中提到算法复杂度可以小于 O(m + n),还在研究中...

时间: 2024-11-05 17:22:32

73. Set Matrix Zeroes的相关文章

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

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

[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.由于第一行第一列自身发生了改变,再用两个变量记录第一

【一天一道LeetCode】#73. Set Matrix Zeroes

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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的数组,如果(i,j)为0,则将第i行第j列全部元素置为0. 这道题目意思很简单,如果考虑