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. nc检测端口是否正常服务的一个命令

    最近碰到一个项目,前端用apache htttpd进行发布(80端口),通过双机负载均衡转发到后端的两个tomcat进行处理(8081和8082端口),现在需要随时监控这三个端口的情况,一旦down掉 ...

  2. TJI读书笔记11-多态

    TJI读书笔记11-多态 再说说向上转型 多态的原理 构造器和多态 协变返回类型 使用继承进行设计 多态是数据抽象和继承之后的第三种基本特征. 一句话说,多态分离了做什么和怎么做(再次对埃大爷佩服的五 ...

  3. Jsoncpp的使用

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式. 易于人阅读和编写.同时也易于机器解析和生成. 它基于JavaScript Programming Lan ...

  4. git服务器的搭建

    http://blog.jobbole.com/25944/ 1,概念 git服务器:就是一个仓储,一个大家都可以访问的公共仓储,大家可以从这个仓储中拉取和推送数据. 协议: 与gist服务通讯的仓储 ...

  5. C# 根据包含文件的路径和文件的名称的字符串获取文件名称的几种方法

    C# 截取带路径的文件名字,扩展名,等等 的几种方法 C#对磁盘IO操作的时候,经常会用到这些,路径,文件,文件名字,文件扩展名. 之前,经常用切割字符串来实现, 可是经常会弄错. 尤其是启始位置,多 ...

  6. oracle 同时更新(update)多个字段多个值

    --创建表A,B: create table A (a1 varchar2(33),a2 varchar2(33),a3 varchar2(33)); create table B (b1 varch ...

  7. CSS3,JS可用于刷新按钮或者加载动画的动画

    html: <input type="button" id="zidong3" style="top: 12px;" /> cs ...

  8. myBatis自动生成mapping,dao和model

    myBatis没有自动封装实体类.所以都需要自己编写,但是表数据过多时.编写难度就会增加,错误率也会增加! 所以myBatis提供mybatis-generator-core-1.3.2-bundle ...

  9. Java核心知识点学习----多线程中的阻塞队列,ArrayBlockingQueue介绍

    1.什么是阻塞队列? 所谓队列,遵循的是先进先出原则(FIFO),阻塞队列,即是数据共享时,A在写数据时,B想读同一数据,那么就将发生阻塞了. 看一下线程的四种状态,首先是新创建一个线程,然后,通过s ...

  10. android开发学习---layout布局、显示单位和如何进行单元测试

    一.五大布局(layout) android中的用五大布局:LinearLayout (线性布局).AbsoluteLayout(绝对布局).RelativeLayout(相对布局).TableLay ...