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

原题链接:https://oj.leetcode.com/problems/set-matrix-zeroes/

题目:给定一个m * n 的矩阵。假设有一个元素是0。将其所在行和列设为0.

思路:先记录下是0 的元素的位置。再去置0.

	public void setZeroes(int[][] matrix) {
boolean firstRowZero = false,firstColumnZero = false;
for(int i=0;i<matrix.length;i++){
if(matrix[i][0] ==0){
firstColumnZero = true;
break;
}
}
for(int i=0;i<matrix[0].length;i++){
if(matrix[0][i] ==0){
firstRowZero = true;
break;
}
}
for(int i=1;i<matrix.length;i++){
for(int j=1;j<matrix[0].length;j++){
if(matrix[i][j] ==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(int i=1;i<matrix.length;i++){
for(int j=1;j<matrix[0].length;j++){
if(matrix[i][0] ==0 || matrix[0][j]==0){
matrix[i][j]=0;
}
}
}
if(firstRowZero){
for(int i=0;i<matrix[0].length;i++)
matrix[0][i]=0;
}
if(firstColumnZero){
for(int i=0;i<matrix.length;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(技巧+逻辑)

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

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

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

  8. Leetcode 细节实现 Set Matrix Zeroes

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

  9. 【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. 在Xampp中添加memache扩展

    1.首先下载phpmemcache,地址为: http://up.2cto.com/2012/0522/20120522094758371.rar 解压下的文件,解压后有以下文件: 接着以管理员身份打 ...

  2. python 中文文档地址总结

    sqlalchemy: https://www.imooc.com/article/details/id/22343

  3. 【Educational Codeforces Round 36 B】Browser

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 分类在区间里面和左边.右边三种情况. 看看l的左边有没有标签.r的右边有没有标签. 就能做完了. [代码] #include < ...

  4. Intellij IDEA中修改项目名称

    如下图红色标识所示: 修改方法见下图:

  5. 项目EasyUi和JS中遇到的问题总汇

    近期因为项目用到EasyUi,曾经仅仅是听过,可是没有详细用过.仅仅能一边学一边做.如今将做的过程中遇到的一些难点总结例如以下,以备后用: EsayUi使用: Json格式:key:value,key ...

  6. hdu5308 I Wanna Become A 24-Point Master(构造)

    题目:pid=5308" target="_blank">http://acm.hdu.edu.cn/showproblem.php? pid=5308 题意:给定 ...

  7. Maven中央仓库信息速查

    http://maven.outofmemory.cn/

  8. NVM安装nodejs的方法

    安装nodejs方式有很多种. 第一种:官网下载  通过nodejs官网下载安装 ,但有个缺陷,不同版本的nodejs无法顺利的切换. 第二种: NVM安装  NVM可以帮助我们快速切换 node版本 ...

  9. 【Codeforces Round #455 (Div. 2) A】Generate Login

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举两个串的前缀长度就好. 组出来. 排序. 取字典序最小的那个. [代码] #include <bits/stdc++.h& ...

  10. 【2017"百度之星"程序设计大赛 - 初赛(B)】小小粉丝度度熊

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6119 [题意] 在这里写题意 [题解] 先把相交的部分合成一个区间. 这个可以用排序,加个简单的处理就能 ...