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

思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0

大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的

void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty())
return; bool iszero1 = false; //第一行是否全0
bool iszero2 = false; //第一列是否全0
//第一行 第一列单独拿出来做标记
for(int j = ; j < matrix[].size(); j++)
{
if(matrix[][j] == ) iszero1 = true;
}
for(int i = ; i < matrix.size(); i++)
{
if(matrix[i][] == ) iszero2 = true;
} for(int i = ; i < matrix.size(); i++)
{
for(int j = ; j < matrix[].size(); j++)
{
//如果数值为0,把对应那一行的第一个 和 那一列的第一个数字置为0
if(matrix[i][j] == )
{
matrix[][j] = ;
matrix[i][] = ;
}
}
} //分行列处理
//先不考虑[0][0] 位置 如果某一行第一个为0,整行置0
for(int i = ; i < matrix.size(); i++)
{
if(matrix[i][] == )
{
for(int j = ; j < matrix[].size(); j++)
{
matrix[i][j] = ;
}
}
} for(int j = ; j < matrix[].size(); j++)
{
if(matrix[][j] == )
{
for(int i = ; i < matrix.size(); i++)
{
matrix[i][j] = ;
}
}
} if(iszero1)
{
for(int j = ; j < matrix[].size(); j++)
{
matrix[][j] = ;
}
}
if(iszero2)
{
for(int i = ; i < matrix.size(); i++)
{
matrix[i][] = ;
}
} return;
}

大神的代码:

public void setZeroes(int[][] matrix) {
int rownum = matrix.length;
if (rownum == ) return;
int colnum = matrix[].length;
if (colnum == ) return; boolean hasZeroFirstRow = false, hasZeroFirstColumn = false; // Does first row have zero?
for (int j = ; j < colnum; ++j) {
if (matrix[][j] == ) {
hasZeroFirstRow = true;
break;
}
} // Does first column have zero?
for (int i = ; i < rownum; ++i) {
if (matrix[i][] == ) {
hasZeroFirstColumn = true;
break;
}
} // find zeroes and store the info in first row and column
for (int i = ; i < matrix.length; ++i) {
for (int j = ; j < matrix[].length; ++j) {
if (matrix[i][j] == ) {
matrix[i][] = ;
matrix[][j] = ;
}
}
} // set zeroes except the first row and column 一起处理的
for (int i = ; i < matrix.length; ++i) {
for (int j = ; j < matrix[].length; ++j) {
if (matrix[i][] == || matrix[][j] == ) matrix[i][j] = ;
}
} // set zeroes for first row and column if needed
if (hasZeroFirstRow) {
for (int j = ; j < colnum; ++j) {
matrix[][j] = ;
}
}
if (hasZeroFirstColumn) {
for (int i = ; i < rownum; ++i) {
matrix[i][] = ;
}
}
}

【leetcode】Set Matrix Zeroes(middle)的更多相关文章

  1. 【leetcode】Spiral Matrix II (middle)

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  2. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. 【leetcode】Factorial Trailing Zeroes(easy)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  6. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  7. 【leetcode】Balanced Binary Tree(middle)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. 【leetcode】 search Insert Position(middle)

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. 【leetcode】Compare Version Numbers(middle)

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. 使用MVVM框架avalon.js实现一个简易日历

    最近在做公司内部的运营管理系统,因为与日历密切相关,同时无需触发条件直接显示在页面上,所以针对这样的功能场景,我就用avalon快速实现了一个简易日历,毕竟也是第一次造日历这种轮子,所以这里记录下我当 ...

  2. PHP中的Memcache详解

    一.Memcache简介 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应 ...

  3. WCF binding的那些事!!!

    原文地址:http://www.cnblogs.com/Anima0My/archive/2008/04/16/1156146.html WCF中常用的binding方式: BasicHttpBind ...

  4. 【Solr】copy字段的应用

    目录 界面查询应用 熟悉Schema.xml copy域的应用 回到顶部 界面查询应用 添加一个文档 查询添加的文档 以上详细介绍了query里面的参数详解. 当不输入任何条件时,进行查询,看看返回结 ...

  5. javascript string 函数集

    JavaScript_String对象说明 string中文为"字符串"的意思,String继承自Object对象,此对象提供字符串的查找操作等函数 JavaScript字符串类型 ...

  6. 【C语言入门教程】2.2 常量 与 变量

    2.2 常量 与 变量 顾名思义,常量是运算中不能改变数值的数据类型,变量是可改变数值的数据类型.根据需要,可将一些在程序中不必改变数值的类型定义为常量,这样也可避免因修改数值造成程序错误.任何改变常 ...

  7. linux C之getchar()非阻塞方式

    参考链接: http://blog.csdn.net/zydlyq/article/details/50963360 #include "../include/CommUart.h" ...

  8. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  9. /etc/bashrc和/etc/profile傻傻分不清楚?

    导读 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 profile来设置用户的工作环境, 很多文章对于 profile 和 bashrc 也都有使用, 但究竟每个文 ...

  10. php中mysql与mysqli的区别

    两个函数都是用来处理DB 的.首先, mysqli 连接是永久连接,而mysql是非永久连接. mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以 ...