【Set Matrix Zeros】cpp
题目:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
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的更多相关文章
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 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 ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- hdu 4740【模拟+深搜】.cpp
题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...
- 【Search Insert Position 】cpp
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- 【First Missing Positive】cpp
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- 【Insertion Sorted List】cpp
题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...
- 【Merge Sorted Array】cpp
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- 【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 ...
随机推荐
- w3cschool中jQuery测试结果总结
1.jQuery 是 W3C 标准. 2.$("div#intro .head") 选择器选取:class="intro" 的任何 div 元素中的首个 id= ...
- javascript的Array.prototype.map()和jQuery的jQuery.map()
两个方法都可以根据现有数组创建新数组,但在使用过程中发现有些不同之处 以下面这个数据为例: var numbers = [1, 3, 4, 6, 9]; 1. 对undefined和null的处理 a ...
- mybatis-分页和缓存
1.分页 1.1在dao接口中配置分页参数: package com.java1234.mappers; import java.util.List;import java.util.Map; imp ...
- char*、string、CString各种字符串之间转换
参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...
- oc语言基础整理
objc.h---------------- typedef struct objc_class *Class; struct objc_object { Class isa OBJC_ISA_AV ...
- 2018.6.13 Java语言基础复习总结
Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...
- C# checked运算符
一.C# checked运算符 checked运算符用于对整型算术运算和显式转换启用溢出检查. 默认情况下,表达式产生的值如果超出了目标类型的范围,将会产生两种情况: ?常数表达式将导致编译时错误. ...
- HTML <input> 标签如何屏蔽浏览器的自动填写?
autocomplete = "off",实测无效. <input type="text" autocomplete = "off"/ ...
- 爬虫学习(八)——带cookie的网页进行爬取
# 前提:# # 通常,很多网站需要登录才能进行浏览,所以在爬取这些网站时,也需要进行登录,并拿取登录时的cookie# # 登录网页,服务器会给客户端一个牌子cookie# # 访问登录页面时,带着 ...
- centos7中使用LVM管理磁盘和挂载磁盘
centos7使用LVM管理一块新的磁盘 注意!文中凡是带#的都是命令标志. 一些重要概念: LV(Logical Volume)- 逻辑卷, VG(Volumne Group)- 卷组, PV(Ph ...