LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)

题目:矩阵置0

难度:Easy

题目内容

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

翻译

给定一个m x n矩阵,如果一个元素是0,就把它的整行和列设为0。

要求:就地置0。

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],
  [3,4,5,2],
  [1,3,1,5]
]
Output:
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

我的思路:因为要求就地,所以不能新建一个矩阵,然后遇见0就将所在行列全部置0

    所以就全部循环一遍,将所有的0的行列下标用List<Integer[]>记录下来,0号位为行,1号位为列。

    然后再循环此List,分别调用置零方法即可。

我的代码:

 1     public void setZeroes(int[][] matrix) {
 2         if (matrix.length == 0 || matrix[0].length == 0)
 3             return;
 4         List<Integer[]> list = new ArrayList<Integer[]>();
 5         for (int i = 0; i < matrix.length; i++) {
 6             for (int j = 0; j < matrix[0].length; j++) {
 7                 if (matrix[i][j] == 0) {
 8                     list.add(new Integer[]{i,j});
 9                 }
10             }
11         }
12         for (Integer[] x : list) {
13             setZero(matrix, x[0], x[1]);
14         }
15     }
16
17     public void setZero(int[][] matrix, int i, int j) {
18         for (int n = 0; n < matrix.length; n++) {
19             matrix[n][j] = 0;
20         }
21         for (int m = 0; m < matrix[0].length; m++) {
22             matrix[i][m] = 0;
23         }
24     }

我的复杂度:O(m*n)+ O((m+n)*x)     x为矩阵中0的个数

编程过程中的问题

1、无

(代码中的Integer[]可以使用int[]替换)

答案代码

 1     public void setZeroes(int[][] matrix) {
 2         if (matrix.length == 0 || matrix[0].length == 0)
 3             return;
 4         boolean row = false,col = false;
 5         for (int i = 0; i < matrix.length; i++) {
 6             for (int j = 0; j < matrix[0].length; j++) {
 7                 if (matrix[i][j] == 0) {
 8                     if (i == 0) row = true;
 9                     if (j == 0) col = true;
10                     matrix[i][0] = 0;
11                     matrix[0][j] = 0;
12                 }
13             }
14         }
15         for (int i = 1; i < matrix.length; i++) {  // 注意此处应该是从1开始,因为第【0,0】如果为0也进行判断的话就会把第0列(标志列)全部设置为0,则接下来整个矩阵都会为0
16             if (matrix[i][0] == 0) {
17                 for (int j = 0; j < matrix[0].length; j++) {
18                     matrix[i][j] = 0;
19                 }
20             }
21         }
22         for (int j = 1; j < matrix[0].length; j++) {  //
23             if (matrix[0][j] == 0) {
24                 for (int i = 0; i < matrix.length; i++) {
25                     matrix[i][j] = 0;
26                 }
27             }
28         }
29         if (row) {
30             for (int j = 0; j < matrix[0].length; j++) {
31                 matrix[0][j] = 0;
32             }
33         }
34         if (col) {
35             for (int i = 0; i < matrix.length; i++) {
36                 matrix[i][0] = 0;
37             }
38         }
39     }

答案复杂度:O(m*n)

答案思路:利用第0行和第0列作为“标志位”,并设置两个boolean变量记录“标志位”是否需要置0,这种方法相对于我的方法在最后置0的时候避免了重复置0的动作,原矩阵再多的0,最也只需要O(m*n)次置0

原文地址:https://www.cnblogs.com/Xieyang-blog/p/9065276.html

时间: 2024-11-06 09:49:58

LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)的相关文章

[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. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(m n) space is probably a bad idea.A simple improvement uses

(每日算法)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. 最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零.这样空间复杂度较高,也是题目不允许的. 题目的难点就在于,如果遇到零元素之后马上在矩阵上操作,将所在的行和列都置为零.在接下来的遍历中,如果你再遇到零,你讲不

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

[CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. LeetCode中的原题,请参见我之前的博客Set Matrix Zeroes 矩阵赋零.

LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题目没有什么难度,但是可以在空间复杂度上做一些处理: 开始写的算法比较简单,将行和列中为0的部分记录下来,然后再经过一个赋值操作: class Solution { public: void setZeroes(vector<vector<int> > &matrix) { if(matr

LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same

LeetCode第[54]题(Java):Spiral Matrix

题目:螺旋矩阵 难度:Medium 题目内容: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 翻译: 给定一个m x n元素的矩阵(m行,n列),以顺时针螺旋顺序返回矩阵的所有元素. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6

[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. 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第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extr