题意:

  给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0。(注:新置的0不必操作)

思路:

  主要的问题是怎样区分哪些是新来的0?

  方法(1):将矩阵复制多一个,根据副本来操作原矩阵。

  方法(2):发现空间还可以用O(n)来解决。

  方法(3):若m[i][j]=0,则将m[i][0]和m[0][j]标记为0,表示i行和j列都为0,但是这样的问题是,首行和首列会冲突?那就将首列先预处理一下,用另外的常量标记就行了。时间复杂度O(n*m),空间O(1)。

  

 class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int col0=, n=matrix.size(), m=matrix[].size();
for(int i=; i<n; i++)
{
if(!matrix[i][]) col0=;
for(int j=; j<m; j++)
{
if(!matrix[i][j])
matrix[i][]=matrix[][j]=;
}
}
for(int i=n-; i>=; i--)
{
for(int j=m-; j>; j--)
{
if(!matrix[][j]||!matrix[i][])
matrix[i][j]=;
}
if(col0==) matrix[i][]=;
}
}
};

AC代码

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

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

  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. jar转dll

    IKVM http://www.cnblogs.com/luckeryin/archive/2012/03/28/2421274.html

  2. 动态链接库dll键盘钩子后台记录代码示例

    //.header #ifndef _DLLHOOK_H_ #define _DLLHOOK_H_ #include <windows.h> #define DLL_EXPORT_FUN ...

  3. bzoj 1934: [Shoi2007]Vote 善意的投票

    #include<cstdio> #include<iostream> #define M 100000 #include<cstring> using names ...

  4. UVALive 5905 Pool Construction 最小割,s-t割性质 难度:3

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  5. select2去除搜索框

    $("#type_select").select2({ minimumResultsForSearch: -1 });

  6. [转载]Android 编译环境 build/envsetup.sh分析

    2013-12-23 11:28:40 转载自: http://blog.csdn.net/evilcode/article/details/7005757 请到转载地址阅读原文, 转载以备查询.

  7. 关于oc运行时 isa指针详解

    Cocoa框架是iOS应用程序的基础,了解Cocoa框架,对开发iOS应用有很大的帮助. 1.Cocoa是什么? Cocoa是OS X和 iOS操作系统的程序的运行环境. 是什么因素使一个程序成为Co ...

  8. Android VersionedGestureDetector手势事件

    今天研究了一下PhotoView,发现里面的自定义的手势事件可以支持所有的SDK版本,该事件可以实现拖拽.滑动.缩放功能.下面直接上代码: public abstract class Versione ...

  9. android 回调函数

    http://blog.csdn.net/xiaanming/article/details/8703708 此为回调的java 实例 http://www.cnblogs.com/qingchen1 ...

  10. 【转】linux下如何查看某个软件 是否安装?安装路径在哪

    以redhat\centos 中php-mysql为例1:如果包是通过yum或者rpm方式安装[root@localhost yum.repos.d]# rpm -qa //找出系统所有的包,找到对应 ...