【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 ...
随机推荐
- 黄学长模拟day1 球的序列
N个编号为1-n的球,每个球都有唯一的编号.这些球被排成两种序列,分别为A.B序列,现在需要重新寻找一个球的序列l,对于这个子序列l中任意的两个球,要求j,k(j<k),都要求满足lj在A中位置 ...
- CPU核数跟多线程的关系
一直以来有这样的疑惑,单核CPU适合多线程吗?是不是几个核的CPU开几个线程是最合适的? 今天就这一问题查了一些资料,现整理如下: 要说多线程就离不开进程,进程和线程的区别在这里就不详细说了,只将关键 ...
- C# switch
要开学了(啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊) 做个沉默的行动者吧!(嘘嘘嘘嘘嘘嘘)今天去水题发现好多基础都不知道啊 1. switch(控制语句) { case 常量表达式:{statement ...
- Android学习笔记(十三)——广播机制
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...
- Laravel5.1-Eloquent ORM:起步
概述 有很多朋友问,MCV的M是在哪里介绍的,这里就是介绍M的地方了.Laravel有一个强大的数据库ORM Eloquent,它的原理是每张数据表对应一个Model,对Model的操作就对应数据库的 ...
- javascript,检测对象中是否存在某个属性
检测对象中属性的存在与否可以通过几种方法来判断. 1.使用in关键字. 该方法可以判断对象的自有属性和继承来的属性是否存在. var o={x:1}; "x" in o; //tr ...
- 12 哈希表相关类——Live555源码阅读(一)基本组件类
12 哈希表相关类--Live555源码阅读(一)基本组件类 这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 ...
- 9 DelayQueueEntry 延时队列节点类——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- SDL播放声音
extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> ...
- git stash简介
原文:http://gitbook.liuhui998.com/4_5.html 一.基本操作 当你正在做一项复杂的工作时, 发现了一个和当前工作不相关但是又很讨厌的bug. 你这时想先修复bug再做 ...