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) {
if(matrix==null){
return;
}
int m=matrix.length;
int n=matrix[0].length; List<Integer> r=new ArrayList<Integer>();
List<Integer> c=new ArrayList<Integer>();
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]==0){
r.add(i);
c.add(j);
}
}
}
for(int i=0; i<r.size(); i++){
int row=r.get(i);
int column=c.get(i);
for(int k=0; k<n; k++){
matrix[row][k]=0;
}
for(int l=0; l<m; l++){
matrix[l][column]=0;
}
}
}
}

二刷:

题目说明不能用额外空间,为了减少重复计算, 我们只是把等于零的相对应的头row和头column标为零。

注意第一排和列共用一个matrix[0][0], 应该分开讨论:

class Solution {
public void setZeroes(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return;
}
int row = matrix.length;
int column = matrix[0].length; //Check first column
boolean isFirstCol = false;
for(int i = 0; i < row; i++){
if (matrix[i][0] == 0){
isFirstCol = true;
}
for(int j = 1; j < column; j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int i = 1; i< row; i++){
for(int j = 1; j < column; j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0){
matrix[i][j] = 0;
}
}
} //check first row
if(matrix[0][0] == 0){
for(int j = 0; j< column; j++){
matrix[0][j] = 0;
}
} //check first column
if(isFirstCol){
for(int i =0; i<row; i++){
matrix[i][0] = 0;
}
}
}
}

LeetCode-Set Matrix Zeroes的更多相关文章

  1. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

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

  3. [leetcode]Set Matrix Zeroes @ Python

    原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...

  4. LeetCode OJ--Set Matrix Zeroes **

    http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...

  5. 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. 原题链接:h ...

  6. LeetCode Set Matrix Zeroes(技巧+逻辑)

    题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...

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

  8. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  9. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  10. 【LeetCode】73. 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 i ...

随机推荐

  1. oracle 用户创建这个挺靠谱

    CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_temp01.dbf ...

  2. DOM4J的使用

    http://blog.csdn.net/redarmy_chen/article/details/12969219 http://blog.csdn.net/wlbing0625/article/d ...

  3. X3850M2安装CertOS 7 KVM 2--VNC

    需要安装远程桌面,否则无鼠标的日子比较难. VNC的安装需要步骤较多,重点参考以下文章: http://www.itzgeek.com/how-tos/linux/centos-how-tos/con ...

  4. DateTime 详解

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  5. LeetCode Reorder List

    struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution ...

  6. windows下无法创建django工程的问题

    环境:python2.7  django1.7 安装好django后,将C:\Python27\Lib\site-packages\Django-1.7.7-py2.7.egg\django\bin; ...

  7. Spring学习笔记(1)——资源加载

    <!-- 占坑,迟点补充底层原理 --> Spring支持4种资源的地址前缀 (1)从类路径中加载资源——classpath: classpath:和classpath:/是等价的,都是相 ...

  8. 2016-08-05(1) ng-options的用法详解

    http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/ng-options-usage/

  9. CentOS7 下 安装 supervisor以及使用

    CentOS7 下 安装 supervisor 以及使用 手动安装 [注] linux环境必须安装 python 1.获取supervisor包:[https://pypi.python.org/py ...

  10. 在Android Studio和Android Eclipse 更改现有项目里的SDK版本

    一,在Eclipse下改项目里的SDK的版本方法有几种,都比较简单:1.右键单击项目--->properties---->Resource----->Android在Project ...