【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 ...
随机推荐
- Mastering Web Application Development with AngularJS 读书笔记(二)
第一章笔记 (二) 一.scopes的层级和事件系统(the eventing system) 在层级中管理的scopes可以被用做事件总线.AngularJS 允许我们去传播已经命名的事件用一种有效 ...
- 清北暑假模拟day1 爱
/* 水题 */ #include<iostream> #include<cstdio> #include<string> #include<cstring& ...
- session 的用法
</head> <body> <?php //session_start();//开启session,必须写在PHP代码最顶端 //HTTP,无状态性 //记录登陆者状态 ...
- OC第三节——NSArray和NSMutableArray
1.OC数组和c数组的区别 C语言的数组: 相同类型变量的有序结合. 类型:可以是简答数据类型.构造数据类型 int a[10 ...
- unity打包模型存在的一个问题
http://blog.csdn.net/leonwei/article/details/39233747 发现U3D的模型打包可能存在一个bug,会导致发布到手机上的模型法线丢失(某些材质下变成全黑 ...
- 谈谈jQuery之绑定事件
http://www.jiangweishan.com/article/jQuery-bind-on.html $.extend({ hook:function(hookName){ var sele ...
- linux中wc命令用法
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...
- SVN版本冲突解决
解决冲突有三种选择: A.放弃自己的更新,使用svn revert(回滚),然后提交.在这种方式下不需要使用svn resolved(解决) B.放弃自己的更新,使用别人的更新.使用最新获取的版本覆盖 ...
- linux 下恢复后台程序的方法
一直以为这个东西不怎么重要,所以一直没怎么去记,已经第三次百度了,不想再有第四次. 如果你在终端下运行一个程序,如果这个程序正在运行,我们可以用 ctrl + z 的命令将这个程序挂到后台. desk ...
- 百度地图API 海量点 自定义添加信息
<!--添加百度地图--> <script type="text/javascript" src="http://api.map.baidu.com/a ...