【Leetcode】【Medium】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.
解题思路:
题目乍一看很简单,将矩阵当前为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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 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 ...
- 【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 ...
- 【LeetCode每天一题】Spiral Matrix II(螺旋数组II)
Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral ord ...
- 【LeetCode每天一题】Spiral Matrix(螺旋打印数组)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 【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. 题解:因为题 ...
随机推荐
- DescriptionResourcePathLocationType Dynamic Web Module 3.0 requires Java
先保证ide的所有jdk都在1.6及以上,如果还是错就试试下面的 在<build></build>中添加 <plugins> <plugin> < ...
- 使用Python来对MySQL数据库进行操作
使用Python对Mysql进行操作,前提是安装了python-Mysql的安装包,安装的方法有多种,可以使用easy_install或者pip 或者是源码进行安装. 下面介绍下如何使用Python ...
- Office 开发版本号与版本对应关系
Office 开发版本号与版本对应关系: office97 : 8.0 office2000 : 9.0 officeXP(2002) : 10.0 office2003 : 11.0 office2 ...
- _Dispose(typeinfo,pointer ); 不知道说的是什么? 感觉会有用, 留待以后研究
[传说]晓不得2013(26562729) 16:45:41别人把文章发出来,说明就是验证过的.[潜水]ひㄨㄨ那个ㄨㄨ(1548253108) 16:46:23[潜水]ひㄨㄨ那个ㄨㄨ(15 ...
- X/Y型文案
[X/Y型文案] X型文案人,他们更像你语言学家.修辞学家和诗人,他们的日常工作就是想创意.查词典和构思修辞,以想办法用华丽的表达来描述产品. Y型文案往往并不华丽,有时甚至只不过是简单地描绘出用户心 ...
- SQLServer 关闭自增长,插入数据
怎样随心所欲的插入自增长的值? 关闭自增长 Demo 有表 [dbo].[tbl_Message] 其中ID是自增的要随意插入ID的值 (前提:这个Id当然是不存在的,存在也可以删除) SET IDE ...
- C语言学习笔记(一)_hello world
一.建立一个文件a.c,写入: #include <stdio.h> //使用printf库函数之前,必须include <stdio.h>int main()//main函数 ...
- 转发(request.setRequestDispacter)和重定向(response.sendRedirect)的区别以及转发的include和forward的区别
//response 重定向的时候,url是会跳转到新的页面的,输入的地址要包含项目名(可以跳到项目之外的页面,比如百度)//request 请求转发的时候,url是不会跳转到新页面的,输入的地址不包 ...
- css2选择器
CSS1&2元素选择器 选择符 类型 版本 简介 * 通配选择符 CSS2 所有元素对象. E 类型(HTML)选择符 CSS1 以文档语言对象类型作为选择符. E#myid id选择符 ...
- canvas 画六边形边框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...