【leetcode】Set Matrix Zeroes(middle)
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)的更多相关文章
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【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 ...
- 【leetcode】Factorial Trailing Zeroes(easy)
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- 【leetcode】Combination Sum III(middle)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode】Insertion Sort List (middle)
Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...
- 【leetcode】Repeated DNA Sequences(middle)★
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【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 ...
- 【leetcode】Compare Version Numbers(middle)
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...
随机推荐
- 深入理解计算机系统-从书中看到了异或交换ab两个值的新感
还得从一个很经典的面试题说起:不通过第三个变量来交换两个变量a,b的值... 一个很经典的答案是通过异或来解决: 第壹步:a=a^b; 第贰步:b=a^b; 第叁步:a=a^b; 以前提起" ...
- iSCSI配置流程
Windows群集两个节点:分别在SQL01和SQL02设置连接共享磁盘: 此前已经在存储服务器通过StarWind创建了三个虚拟磁盘:Quemon+data+backup:starwind安装请参考 ...
- 【C语言入门教程】7.5 枚举
在实际应用中,有的变量只有几种可能取值.如人的性别只有两种可能取值,星期只有七种可能取值.在 C 语言中对这样取值比较特殊的变量可以定义为枚举类型.所谓枚举是指将变量的值一一列举出来,变量只限于列举出 ...
- 基于Windows10安装Ubuntu双系统
步骤: 1.从Ubuntu的官网上下载Ubuntu的iSO安装包. http://www.ubuntu.com/download/ 我安装的版本是Ubuntu 14.04.3 LTS 64位版本 2. ...
- 我们为之奋斗过的C#之---简单的库存管理系统
今天非常开心,因为今天终于要给大家分享一个库存管理项目了. 我个人感觉在做项目之前一定先要把逻辑思路理清,不要拿到项目就噼里啪啦的一直敲下去这样是一不好的习惯,等你做大项目的时候,你就不会去养成一种做 ...
- updatepanel用法之triggers(局部刷新,全部刷新)使用示例
asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepanel内部的内容postbacktrigger(普通回调触发器):全部刷新 <asp:ScriptMa ...
- Promise 原理探究及其简单实现
可移步 http://donglegend.com/2016/09/11/promise%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6/ 观看 Promise是个什么玩意,大 ...
- PHPCMS几个有用的全局函数
1.$site_setting = get_site_setting($siteid); 这个get_site_setting()函数读取的是多站点中$siteid站点的相关配置,具体位置在网站根 ...
- 使用group_concat 时,设置mysql默认的长度
SHOW VARIABLES LIKE "group_concat_max_len"; SET GLOBAL group_concat_max_len=1024000; SET ...
- 通过Unity3d创建二维码(利用zxing2.2)
http://blog.csdn.net/liulala16/article/details/14521979 2013-11-08 14:53 1965人阅读 评论(3) 收藏 举报 首先 下载ZX ...