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

posted on 2018-05-20 23:58  清风吹斜阳  阅读(436)  评论(0编辑  收藏  举报

导航