题目

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. 【转】This Android SDK requires Android Developer Toolkit version 20.0.0 or above

    本人最近在操作更新ANDROID SDK时出现类似于题目中的错误,是一启动ECLIPSE时.但是,我现在只是想恢复到原先的开发环境.于是找到本文,方法有效!!! windows 下面安装Android ...

  2. ArcGIS for Android 10.1.1API 中文标注导致程序异常崩溃问题

    1.前言 问题:在部分Android机型中使用ArcGIS for Android 10.1.1 API 中文标注导致程序异常崩溃. 说明:手里有两台机器一台是Nexus4,原生系统,版本4.4.4, ...

  3. PHP报错configure error Cannot find libmysqlclient under usr

    编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法 (问题产生,mysql是yum安装的,libmysqlclient* ...

  4. Windows系统HTTP身份验证方法

    当Windows客户端尝试使用HTTP协议访问基于Web的资源时,会在客户端和服务器之间建立"对话".换句话说,服务器告诉客户端,访问资源之前进行身份验证 ,并且服务器还告诉客户端 ...

  5. python之字符串切割

    Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...

  6. int _tmain(int argc, _TCHAR* argv[])

    int _tmain(int argc, _TCHAR* argv[]){ int i; for (i = 0; i<argc; i++) cout<<argv[i]<< ...

  7. ORACLE的raw属性

    网上说RAW类型在网络数据传送的时候可以避免字节的字符集转换,在mssql中使用的GUID类型在oracle中对应的也是raw类型(一般是raw(16)),如果此时使用连接查询将raw类型的字段和va ...

  8. window下安装ubuntu(ubuntu可删除)

    进入ububtu13.04的安装界面,这里我们选择了“中文(简体)”,然后单击安装: 下图是现场拍的:   出现如下图时,请根据需要选择,然后单击“继续” , 接下来会出现问你是否要连接网络,我们选择 ...

  9. CSS3和动画

    定位: z-index叠层    数字越大越往上层 注意:要用z-index属性必须设position属性 溢出:overflow 属性值:visible    不剪切内容也不添加滚动条 Auto   ...

  10. 第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

    需求:python如何实现普通用户登录服务器后切换到root用户再执行命令 解决参考: 代码: def verification_ssh(host,username,password,port,roo ...