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. java笔记1

    第五天学习笔记 1.面对对象的理解并举例? 面对对象的核心:找合适的对象做合适的事情. 面对对象编程的思想:尽可能的用计算机语言来描述现实生活中的事物. 面对对象:侧重于对象 2.类与对象之间的关系? ...

  2. CRC

    #define POLY 0x1021 /** * Calculating CRC-16 in 'C' * @para addr, start of data * @para num, length ...

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

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

  4. 安装和部署ZkeaCMS

    ZkeaCMS是基于EasyFrameWork,使用ASP.NET MVC4开发的开源CMS. ZkeaCMS一个内容管理软件(网站).ZkeaCMS不仅只是管理内容,更是重新定义了布局.页面和组件, ...

  5. X/Y型文案

    [X/Y型文案] X型文案人,他们更像你语言学家.修辞学家和诗人,他们的日常工作就是想创意.查词典和构思修辞,以想办法用华丽的表达来描述产品. Y型文案往往并不华丽,有时甚至只不过是简单地描绘出用户心 ...

  6. Tara's Beautiful Permutations 组合数学

    https://www.hackerrank.com/contests/hourrank-15/challenges/taras-beautiful-permutations 首先先统计一下个数为2的 ...

  7. A 浪哥的烦恼 完全背包dp

    https://biancheng.love/contest-ng/index.html#/131/problems 首先,去到n点的最小时间是所有数加起来. 然后,如果我1 --- 2,然后再2-- ...

  8. C#拼接地图瓦片

    为了在AE程序中使用离线的电子地图,思路如下: 利用下载工具下载地图切片,然后利用C#进行切片拼接成一张图片,最后使用ArcMap进行地理配准,然后发布成ArcGIS Server 切片服务供程序使用 ...

  9. [转载]Altium规则详解及设置

    在Altium中进行PCB的设计时,经常会使用规则(Rule)来进行限定以确定线宽孔径等参数,此文将简要的介绍规则中的一些标量代表了什么. Electrical——电气规则.安全间距,线网连接等 Ro ...

  10. SAP 禁止某个库位的货物移动

    SAP 禁止某个库位的货物移动 1.先去spro --> 物料管理 --> 库存管理和实际库存 --> 权限管理 --> 授权检查存储位置 将要禁止的库位后的权限勾选上, 2. ...