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

解题思路:

题目乍一看很简单,将矩阵当前为0的位的行列都置为0;

问题在于:当遇到一个已知0时,不能立刻将其行列置0,因为这样会把原本不是0的位更改,导致继续遍历的时候无法区分哪些是初始0,哪些是后改0;

有一个解决方法,就是先遍历所有位,记录所有初始为0的行列坐标,遍历一遍之后,再统一做更改;

但是这个解决方法会占用额外的空间,如何使用o(1)空间完成置0的任务?

解决方法:

1、先找到一个初始0位置,并记录它的行oi和列oj;

2、oi行和oj列最终需要置零,索性将oi这一行和oj这一列当做记录数组;

3、遍历所有位置,如果遇到0,则将其i和j对应的(oi, j)和(i, oj)置0,这样不会多置0,也做到了标记的作用;

4、最终遍历oi这一行,将值为0的位置所在列置0;同理遍历oj这一列的元素,将其值为0的位置所在行置0;

代码:

 class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
if (m == )
return;
int n = matrix[].size();
int oi = -, oj = -;
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (matrix[i][j] == ) {
oi = i;
oj = j;
break;
}
}
if (oi != -) break;
}
if (oi == -) return; for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (matrix[i][j] == ) {
matrix[oi][j] = ;
matrix[i][oj] = ;
}
}
} for (int i = ; i < m; ++i) {
if (i != oi && matrix[i][oj] == ) { // notice
for (int j = ; j < n; ++j)
matrix[i][j] = ;
}
} for (int j = ; j < n; ++j) {
if (matrix[oi][j] == ) {
for (int i = ; i < m; ++i)
matrix[i][j] = ;
}
} for (int j = ; j < n; ++j)
matrix[oi][j] = ; return;
}
};

【Leetcode】【Medium】Set Matrix Zeroes的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

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

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  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(设置0矩阵)

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

  7. 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)

    Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...

  8. 【LeetCode每天一题】Spiral Matrix(螺旋打印数组)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. 【leetcode刷题笔记】Spiral Matrix II

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

  10. 【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. 题解:因为题 ...

随机推荐

  1. js 小工具-- 获取主机名

    <script type="text/javascript"> function getHostName(url) { var host = "null&qu ...

  2. 自己开发基于c#的垂直滚动条控件

    由于Visual Studio工具箱中自带滚动条控件外观太老,而且没有颜色外观属性可设置. 所以自己就试着开发一个垂直的滚动条,它可以用来控制TextBox的滚动. 由于代码比较多,源文件已经打包到网 ...

  3. js 更改head的title

    使用document.title = "hello"; 不能使用 $("title").text("dd");或者            $ ...

  4. dubbo+zookeeper例子

    0.原理   Alibaba有好几个分布式框架,主要有:进行远程调用(类似于RMI的这种远程调用)的(dubbo.hsf),jms消息服务(napoli.notify),KV数据库(tair)等.这个 ...

  5. 深入理解js——prototype原型

    之前(深入理解js--一切皆是对象)中说道,函数也是一种对象.它也是属性的集合,你也可以对函数进行自定义属性.而JavaScript默认的给了函数一个属性--prototype(原型).每个函数都有一 ...

  6. 《Linux内核设计与实现》读书笔记 第一章 Linux内核简介

    一.相关历史 1. Unix内核的特点 简洁:仅提供系统调用并有一个非常明确的设计目的 抽象:几乎所有东西都被当做文件 可移植性:使用C语言编写,使得其在各种硬件体系架构面前都具备令人惊异的移植能力 ...

  7. js 获取 通过 ”?“ 或者 ”&“ url 传过来参数值

    请先 引用 jQuery的js <script> String.prototype.GetValue=function(para) { var reg = new RegExp(" ...

  8. ZMQ 在linux进程 和分布式之间的通信

    ZMQ 在linux进程 和分布式之间的通信 待补全

  9. [转载] 1. JebAPI 之 jeb.api

    本文转载自: https://www.zybuluo.com/oro-oro/note/142707 JEB API 官方地址:https://www.pnfsoftware.com/apidoc/  ...

  10. TextField 的文字间距

    #import "XZCTextField.h" @implementation XZCTextField ////控制清除按钮的位置//-(CGRect)clearButtonR ...