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. android 分享或者调用系统或者其他app时 应注意! startActivityForResult() 使用

    //判断是否有相应的Activity来接受intentPackageManager packageManager = getPackageManager();List<ResolveInfo&g ...

  2. 黑马程序员——OC语言 类和对象

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)类 1)类的声明 代码编写 ①定义一个Car类,拥有2个属性:轮子数 ...

  3. 作业七:团队项目——Alpha版本冲刺阶段 001

    今天进展:准备开发环境,安装软件. 今天安排:因为软件过于庞大,所以我们第一天都在按软件,原本计划第一天要设计框架,但因为软件问题.所以我们决定留到第二天.

  4. Android图片加载与缓存开源框架:Android Glide

    <Android图片加载与缓存开源框架:Android Glide> Android Glide是一个开源的图片加载和缓存处理的第三方框架.和Android的Picasso库类似,个人感觉 ...

  5. JS三元

    ((productDatas[i].Img.indexOf("http") == -1) ? ("/upload/190-160/" + productData ...

  6. error CS0103: 当前上下文中不存在名称“ViewBag”

    error CS0103: 当前上下文中不存在名称“ViewBag” View文件夹下缺少web.config文件

  7. KVC/KVO原理详解及编程指南

    一.简介 1.KVC简介 2.KVO简介 二.KVC相关技术 1.Key和Key Path 2.点语法和KVC 3.一对多关系(To-Many)中的集合访问器方法 4.键值验证(Key-Value V ...

  8. ubuntu14.04LTS编译MUDOS v22.2b14

    附:MudOS v22.2b14下载连接 简单的编译步骤: (1)运行./build.MudOS (2)vim socket_efuns.c       o 修改Ln:1198的代码为         ...

  9. setImageBitmap和setImageResource

    同样的布局文件,小分辨率手机: 1.使用setImageBitmap设置时,出现如下现象:  2.使用setImageResource时,图片显示正常 原因: setImageResource(id) ...

  10. vim替换指令备忘

    1.  替换当前行中的内容: :s/from/to/    (s即substitude) :s/from/to/     :  将当前行中的第一个from,替换成to.如果当前行含有多个        ...