题目

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

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

代码

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
const size_t size_row = matrix.size();
const size_t size_col = matrix[].size();
bool if_row0_has_zero = false;
bool if_col0_has_zero = false;
for (size_t col = ; col < size_col; ++col){
if (matrix[][col]==){
if_row0_has_zero = true;
break;
}
}
for (size_t row = ; row < size_row; ++row){
if (matrix[row][]==){
if_col0_has_zero = true;
break;
}
}
for (size_t row = ; row < size_row; ++row){
for (size_t col = ; col < size_col; ++col){
if (matrix[row][col]==){
matrix[row][] = ;
matrix[][col] = ;
}
}
}
for (size_t row = ; row < size_row; ++row){
for (size_t col = ; col < size_col; ++col){
if ( matrix[row][]== || matrix[][col]== ) matrix[row][col]=;
}
}
if (if_row0_has_zero) {
for (size_t col = ; col < size_col; ++col) matrix[][col]=;
}
if (if_col0_has_zero){
for (size_t row = ; row < size_row; ++row) matrix[row][]=;
}
}
};

Tips:

1. 算法时间复杂度上是O(n²)

2. 这里为了节省空间复杂度,用到的技巧是把第一行和第一列作为该行或该列是否含有0元素的标志位,这样就可以在常数空间内完成题目。

========================================================

第二次过这道题,思路比较清晰,代码也一次AC了。

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size()==) return;
// check first row
bool first_row_zero = false;
for ( int j=; j<matrix[].size(); ++j ) {
if (matrix[][j]==) {first_row_zero=true;break;}
}
// check frist col
bool first_col_zero = false;
for ( int j=; j<matrix.size(); ++j ) {
if ( matrix[j][]==) {first_col_zero=true;break;}
}
// check remains
for ( int i=; i<matrix.size(); ++i )
{
for ( int j=; j<matrix[i].size(); ++j )
{
if ( matrix[i][j]== )
{
matrix[][j] = ;
matrix[i][] = ;
}
}
}
// set row zeros
for ( int i=; i<matrix.size(); ++i )
{
if ( matrix[i][]== )
{
for ( int j=; j<matrix[i].size(); ++j ) matrix[i][j] = ;
}
}
// set col zeros
for ( int j=; j<matrix[].size(); ++j )
{
if ( matrix[][j]== )
{
for ( int i=; i<matrix.size(); ++i ) matrix[i][j] = ;
}
}
if (first_row_zero)
{
for ( int j=; j<matrix[].size(); ++j ) matrix[][j] = ;
}
if ( first_col_zero)
{
for ( int i=; i<matrix.size(); ++i ) matrix[i][] = ;
}
}
};

这个代码大体上没有问题,但是在一个部分是可以优化的。就是set row zeros和set col zeros两个部分可以合成一个,改一版代码如下。

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
if (matrix.size()==) return;
// check first row
bool first_row_zero = false;
for ( int j=; j<matrix[].size(); ++j ) {
if (matrix[][j]==) {first_row_zero=true;break;}
}
// check frist col
bool first_col_zero = false;
for ( int j=; j<matrix.size(); ++j ) {
if ( matrix[j][]==) {first_col_zero=true;break;}
}
// check remains
for ( int i=; i<matrix.size(); ++i )
{
for ( int j=; j<matrix[i].size(); ++j )
{
if ( matrix[i][j]== )
{
matrix[][j] = ;
matrix[i][] = ;
}
}
}
// set zeros
for ( int i=; i<matrix.size(); ++i )
{
for ( int j=; j<matrix[i].size(); ++j )
{
if ( matrix[i][]== || matrix[][j]== ) matrix[i][j]=;
}
}
if (first_row_zero)
{
for ( int j=; j<matrix[].size(); ++j ) matrix[][j] = ;
}
if ( first_col_zero)
{
for ( int i=; i<matrix.size(); ++i ) matrix[i][] = ;
}
}
};

这样改版后,代码效率提升了。

【Set Matrix Zeros】cpp的更多相关文章

  1. 【Spiral Matrix II】cpp

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

  2. leetcode 【 Set Matrix Zeroes 】python 实现

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

  3. 【Longest Palindromic Substring】cpp

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  4. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  5. 【Search Insert Position 】cpp

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

  6. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  7. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

  8. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  9. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

随机推荐

  1. c语言数据结构:用标志位实现循环队列

    #include<stdio.h> #include<stdlib.h> #define MAXSIZE 10//定义队列长度 ;//定义标志位 typedef struct ...

  2. instanceof 关键字

    boolean = Object(类引用名) instanceof  Class(类名) 作用:判断符号左边的引用指向的对象是否是右边这个类的对象:

  3. C# DateTime的时区

    C#中可以通过DateTime的Kind属性指定DateTime的时区 DateTimeKind有3个枚举值: Unspecified:未指定为UTC时间或本地时间 Utc: UTC时间 Local: ...

  4. jQuery-动画animate() 方法操作 CSS 属性

    语法: $(selector).animate({params},speed,callback); 多个params 之间用逗号(,)隔开. 必须使用 Camel 标记法书写所有的属性名,比如,必须使 ...

  5. shell 快速浏览

    总结自: https://github.com/qinjx/30min_guides/blob/master/shell.md: http://blog.itpub.net/14293828/view ...

  6. IOS - (id)initWithStyle... 方法的使用

    // 该方法只有在通过代码创建控件的时候才会调用, 如果控件是通过xib或者storyboard创建出来的不会调用该方法- (id)initWithStyle:(UITableViewCellStyl ...

  7. POJ 3281 Dining(网络流最大匹配)

    分析: 数学模型是三个集合A,B,C,(a,b,c)构成一个匹配.因为图一个点只能匹配一次,把a拆点a',a", 在可以匹配的点上连边,s - b - a' - a" - c - ...

  8. NFS服务器实现文件共享

    NFS服务器运行原理 实战配置NFS服务器 配置Samba服务器及实现文件共享 (一)NFS器服务端描述 NFS服务器: Network File System,网络文件系统使FreeBSD支持的一种 ...

  9. 2018.5.22 Oracle安装配置在虚拟机中外部电脑连接服务

    1.拷贝老师的集成文件(win系统和oracle服务) 2.安装虚拟机,并且打开镜像文件 3.启动监听程序(实例服务[自动].监听服务) 4.查看虚拟机ip,此ip要主机ip在同一个网段 5.检查虚拟 ...

  10. Scanner和 Random类,控制语句的例题,商品管理(直接赋值)

    Scanner类的使用: import java.util.Scanner; class Demo02 { public static void main(String[] args) { //1.导 ...