Set Matrix Zeroes

(Link: https://oj.leetcode.com/problems/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 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?

思路: 找到第一个 0 元素,记下其行和列。然后对其他 0 元素,分别将其投影在记下的行和列上(做标记)。遍历之后,对于所有行中的标记,将其所在列都置为 0; 对于所有列中标记,将其所在行都置为 0. (最后置标记的行和列为 0. 可含在上述步骤) 时间: O(n2), 空间 : O(1)

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(!matrix.size() || !matrix[0].size()) return;
const int INF = 1001;
int row = matrix.size(), col = matrix[0].size();
int u = -1, v = -1;
for(int r = 0; r < row; ++r)
for(int c = 0; c < col; ++c) {
if(matrix[r][c] == 0) {
if(u == -1) { u = r, v = c; continue; }
matrix[u][c] = INF;
matrix[r][v] = INF;
}
}
if(u == -1) return;
if(matrix[u][v] == INF) matrix[u][v] = 0;
for(int c = 0; c < col; ++c) {
if(matrix[u][c] == INF) {
for(int i = 0; i < row; ++i) matrix[i][c] = 0;
} else matrix[u][c] = 0;
}
for(int r = 0; r < row; ++r) {
if(matrix[r][v] == INF) {
for(int j = 0; j < col; ++j) matrix[r][j] = 0;
} else matrix[r][v] = 0;
}
}
};

55. Set Matrix Zeroes的更多相关文章

  1. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

  2. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  3. 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 ...

  4. 【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 ...

  5. 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 ...

  6. 【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. 思路:不能用 ...

  7. Set Matrix Zeroes leetcode java

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

  8. LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]

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

  9. [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 ...

随机推荐

  1. cocos2d-x 系列文章介绍

    学习 cocos2d-x 一年多,从3.0bata 到 现在的 3.6 ,从最早没什么教程到现在官网繁多的资料教程,  cocos2d-x  的变化实在是大.刚开始学习 cocos2d-x 是到处找资 ...

  2. mybatis sql in 查询

    mybatis官方学习文档:http://www.mybatis.org/core/getting-started.html 本文转自:http://www.blogjava.net/xmatthew ...

  3. windows 版的julia repl 启动时间已经大大优化!

    julia 是一门语法类似python 偏向主要用于科学计算的语言,julia吸收了很多其它语言的优点,内置了大量函数,使用起来很方便. 之前windows下的 julia repl(交互解释器)启动 ...

  4. 基于NodeJS的全栈式开发

    前言 为了解决传统Web开发模式带来的各种问题,我们进行了许多尝试,但由于前/后端的物理鸿沟,尝试的方案都大同小异.痛定思痛,今天我们重新思考了“前后端”的定义,引入前端同学都熟悉的 NodeJS,试 ...

  5. C++ iostream的线程安全性问题

    标准C里面的printf, fprintf之类的,会给FILE指针上锁(在unix平台上就是记录锁,按照msdn的说法windows上也有类似的锁),所以单次函数调用总是线程安全的: 要注意,这里只对 ...

  6. Activity 和 生命周期: 创建

    了解了整体的android创建流程之后,就分析一下到底这个过程中做了什么? activity创建中开始时由activityStack中的realstartActivityLocked函数中调用了act ...

  7. Java数据结构和算法之栈与队列

    二.栈与队列 1.栈的定义 栈(Stack)是限制仅在表的一端进行插入和删除运算的线性表. (1)通常称插入.删除的这一端为栈顶(Top),另一端称为栈底(Bottom). (2)当表中没有元素时称为 ...

  8. ARM compiler No such file or directory

    /********************************************************************************* * ARM compiler No ...

  9. c++标准模板库algorithm头文件中accumulate算法的代码

    template <typename T>T algorithm(T* start, T* end, T total)//把[start, end)标记范围内所有元素累加到total中{  ...

  10. SQL Server中常用的SQL语句(转):

    SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...