码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Set Matrix Zeros

时间:2014-09-09 10:38:48      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   sp   log   

 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 }

 

[LeetCode] Set Matrix Zeros

标签:style   blog   color   os   io   for   div   sp   log   

原文地址:http://www.cnblogs.com/yuhaos/p/3961286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!