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. 作业七:团队项目——Alpha版本冲刺阶段008

    今日进度:组内成员讨论 今日安排:组内成员分工

  2. C# WebBrowser控件使用教程与技巧收集

    常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...

  3. css3弹性盒子温习

    弹性盒子由弹性容器(Flex container)和弹性子元素(Flex item)组成. 弹性容器通过设置 display 属性的值为 flex 或 inline-flex将其定义为弹性容器. 弹性 ...

  4. 安全协议系列(五)---- IKE 与 IPSec(中)

    在上一篇中,搭建好了实验环境.完整运行一次 IKE/IPSec 协议,收集相关的输出及抓包,就可以进行协议分析.分析过程中,我们将使用 IKE 进程的屏幕输出和 Wireshark 抓包,结合相关 R ...

  5. 触发Full GC执行的情况

    除直接调用System.gc外,触发Full GC执行的情况有如下四种. 1. 旧生代空间不足 旧生代空间只有在新生代对象转入及创建为大对象.大数组时才会出现不足的现象,当执行Full GC后空间仍然 ...

  6. android自定义按键

    android自带菜单键.返回键.搜索键的重写 转自:http://blog.sina.com.cn/s/blog_7cb9b3b801015yk8.html   返回键 public void on ...

  7. NUBWO/狼博旺 NO-3000台式电脑耳机头戴式游戏电竞语音耳麦带话筒

    产品名称:NUBWO/狼博旺 NO-3000 套餐类型: 官方标配 颜色分类: 蓝色耳机(发光) 黑蓝7.1声道USB 黑色 黑蓝色(发光) 黑蓝色 黑蓝单插孔 白色(发光) 白色 佩戴方式: 头戴护 ...

  8. 使用PowerDesigner导出MySQL数据库建模

    数据库服务器在linux上面,客户端本子win8系统 1.安装odbc 1.1 下载odbc,http://dev.mysql.com/downloads/connector/odbc/  Windo ...

  9. C# 的各种排序

    http://www.cnblogs.com/jiajiayuan/category/302446.html

  10. How to do code coverage test for windows service

    First, instrument the exe or dll by command vsinstr -coverage the dll/exe second, start the performa ...