[LeetCode] 73. 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.
Example 1:
- Input:
- [
- [1,1,1],
- [1,0,1],
- [1,1,1]
- ]
- Output:
- [
- [1,0,1],
- [0,0,0],
- [1,0,1]
- ]
Example 2:
- Input:
- [
- [0,1,2,0],
- [3,4,5,2],
- [1,3,1,5]
- ]
- Output:
- [
- [0,0,0,0],
- [0,4,5,0],
- [0,3,1,0]
- ]
Follow up:
- 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?
给一个m x n的矩阵,如果一个元素是0,就把它所在的行和列都设成0,用in place做。
解法1: 新建一个矩阵,然后一行一行的扫,只要有0,就将新建的矩阵的对应行全赋0,行扫完再扫列,然后把更新完的矩阵赋给matrix。空间复杂度为O(mn)。
解法2: 用一个长度为m的一维数组记录各行中是否有0,用一个长度为n的一维数组记录各列中是否有0,最后直接更新matrix数组即可。空间复杂度为O(m+n),
解法3: 这道题要求用常数级空间复杂度O(1),不能新建数组,就用原数组的第一行第一列来记录各行各列是否有0.
- 先扫描第一行第一列,如果有0,则将各自的flag设置为true
- 然后扫描除去第一行第一列的整个数组,如果有0,则将对应的第一行和第一列的数字赋0
- 再次遍历除去第一行第一列的整个数组,如果对应的第一行和第一列的数字有一个为0,则将当前值赋0
- 最后根据第一行第一列的flag来更新第一行第一列
时间复杂度是O(m*n), 三种方法都一样,需要进行两次扫描,一次确定行列置0情况,一次对矩阵进行实际的置0操作。
Java:
- void setZeroes(vector<vector<int> > &matrix) {
- int col0 = 1, rows = matrix.size(), cols = matrix[0].size();
- for (int i = 0; i < rows; i++) {
- if (matrix[i][0] == 0) col0 = 0;
- for (int j = 1; j < cols; j++)
- if (matrix[i][j] == 0)
- matrix[i][0] = matrix[0][j] = 0;
- }
- for (int i = rows - 1; i >= 0; i--) {
- for (int j = cols - 1; j >= 1; j--)
- if (matrix[i][0] == 0 || matrix[0][j] == 0)
- matrix[i][j] = 0;
- if (col0 == 0) matrix[i][0] = 0;
- }
- }
Java:
- public void setZeroes(int[][] matrix) {
- boolean fr = false,fc = false;
- for(int i = 0; i < matrix.length; i++) {
- for(int j = 0; j < matrix[0].length; j++) {
- if(matrix[i][j] == 0) {
- if(i == 0) fr = true;
- if(j == 0) fc = true;
- matrix[0][j] = 0;
- matrix[i][0] = 0;
- }
- }
- }
- for(int i = 1; i < matrix.length; i++) {
- for(int j = 1; j < matrix[0].length; j++) {
- if(matrix[i][0] == 0 || matrix[0][j] == 0) {
- matrix[i][j] = 0;
- }
- }
- }
- if(fr) {
- for(int j = 0; j < matrix[0].length; j++) {
- matrix[0][j] = 0;
- }
- }
- if(fc) {
- for(int i = 0; i < matrix.length; i++) {
- matrix[i][0] = 0;
- }
- }
- }
Java:
- public class Solution {
- public void setZeroes(int[][] matrix) {
- boolean firstRowZero = false;
- boolean firstColumnZero = false;
- //set first row and column zero or not
- for(int i=0; i<matrix.length; i++){
- if(matrix[i][0] == 0){
- firstColumnZero = true;
- break;
- }
- }
- for(int i=0; i<matrix[0].length; i++){
- if(matrix[0][i] == 0){
- firstRowZero = true;
- break;
- }
- }
- //mark zeros on first row and column
- for(int i=1; i<matrix.length; i++){
- for(int j=1; j<matrix[0].length; j++){
- if(matrix[i][j] == 0){
- matrix[i][0] = 0;
- matrix[0][j] = 0;
- }
- }
- }
- //use mark to set elements
- for(int i=1; i<matrix.length; i++){
- for(int j=1; j<matrix[0].length; j++){
- if(matrix[i][0] == 0 || matrix[0][j] == 0){
- matrix[i][j] = 0;
- }
- }
- }
- //set first column and row
- if(firstColumnZero){
- for(int i=0; i<matrix.length; i++)
- matrix[i][0] = 0;
- }
- if(firstRowZero){
- for(int i=0; i<matrix[0].length; i++)
- matrix[0][i] = 0;
- }
- }
- }
Java:
- public void setZeroes(int[][] matrix) {
- if(matrix==null || matrix.length==0 || matrix[0].length==0)
- return;
- boolean rowFlag = false;
- boolean colFlag = false;
- for(int i=0;i<matrix.length;i++)
- {
- if(matrix[i][0]==0)
- {
- colFlag = true;
- break;
- }
- }
- for(int i=0;i<matrix[0].length;i++)
- {
- if(matrix[0][i]==0)
- {
- rowFlag = true;
- break;
- }
- }
- for(int i=1;i<matrix.length;i++)
- {
- for(int j=1;j<matrix[0].length;j++)
- {
- if(matrix[i][j]==0)
- {
- matrix[i][0] = 0;
- matrix[0][j] = 0;
- }
- }
- }
- for(int i=1;i<matrix.length;i++)
- {
- for(int j=1;j<matrix[0].length;j++)
- {
- if(matrix[i][0]==0 || matrix[0][j]==0)
- matrix[i][j] = 0;
- }
- }
- if(colFlag)
- {
- for(int i=0;i<matrix.length;i++)
- {
- matrix[i][0] = 0;
- }
- }
- if(rowFlag)
- {
- for(int i=0;i<matrix[0].length;i++)
- {
- matrix[0][i] = 0;
- }
- }
- }
Python:
- class Solution:
- # @param matrix, a list of lists of integers
- # RETURN NOTHING, MODIFY matrix IN PLACE.
- def setZeroes(self, matrix):
- first_col = reduce(lambda acc, i: acc or matrix[i][0] == 0, xrange(len(matrix)), False)
- first_row = reduce(lambda acc, j: acc or matrix[0][j] == 0, xrange(len(matrix[0])), False)
- for i in xrange(1, len(matrix)):
- for j in xrange(1, len(matrix[0])):
- if matrix[i][j] == 0:
- matrix[i][0], matrix[0][j] = 0, 0
- for i in xrange(1, len(matrix)):
- for j in xrange(1, len(matrix[0])):
- if matrix[i][0] == 0 or matrix[0][j] == 0:
- matrix[i][j] = 0
- if first_col:
- for i in xrange(len(matrix)):
- matrix[i][0] = 0
- if first_row:
- for j in xrange(len(matrix[0])):
- matrix[0][j] = 0
Python:
- class Solution:
- # @param {integer[][]} matrix
- # @return {void} Do not return anything, modify matrix in-place instead.
- def setZeroes(self, matrix):
- m = len(matrix)
- if m == 0:
- return
- n = len(matrix[0])
- row_zero = False
- for i in range(m):
- if matrix[i][0] == 0:
- row_zero = True
- col_zero = False
- for j in range(n):
- if matrix[0][j] == 0:
- col_zero = True
- for i in range(1, m):
- for j in range(1, n):
- if matrix[i][j] == 0:
- matrix[i][0] = 0
- matrix[0][j] = 0
- for i in range(1, m):
- if matrix[i][0] == 0:
- for j in range(1, n):
- matrix[i][j] = 0
- for j in range(1, n):
- if matrix[0][j] == 0:
- for i in range(1, m):
- matrix[i][j] = 0
- if col_zero:
- for j in range(n):
- matrix[0][j] = 0
- if row_zero:
- for i in range(m):
- matrix[i][0] = 0
C++:
- class Solution {
- public:
- void setZeroes(vector<vector<int> > &matrix) {
- if (matrix.empty() || matrix[0].empty()) return;
- int m = matrix.size(), n = matrix[0].size();
- bool rowZero = false, colZero = false;
- for (int i = 0; i < m; ++i) {
- if (matrix[i][0] == 0) colZero = true;
- }
- for (int i = 0; i < n; ++i) {
- if (matrix[0][i] == 0) rowZero = true;
- }
- for (int i = 1; i < m; ++i) {
- for (int j = 1; j < n; ++j) {
- if (matrix[i][j] == 0) {
- matrix[0][j] = 0;
- matrix[i][0] = 0;
- }
- }
- }
- for (int i = 1; i < m; ++i) {
- for (int j = 1; j < n; ++j) {
- if (matrix[0][j] == 0 || matrix[i][0] == 0) {
- matrix[i][j] = 0;
- }
- }
- }
- if (rowZero) {
- for (int i = 0; i < n; ++i) matrix[0][i] = 0;
- }
- if (colZero) {
- for (int i = 0; i < m; ++i) matrix[i][0] = 0;
- }
- }
- };
All LeetCode Questions List 题目汇总
[LeetCode] 73. Set Matrix Zeroes 矩阵赋零的更多相关文章
- [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 ...
- [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 ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- [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 ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] 73. 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. Follow ...
- Leetcode73. Set Matrix Zeroes矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输 ...
- Leetcode#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
- 【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 ...
随机推荐
- jquery 内容筛选选择器
基本筛选选择器针对的都是元素DOM节点,如果我们要通过内容来过滤,jQuery也提供了一组内容筛选选择器,当然其规则也会体现在它所包含的子元素或者文本内容上 注意事项: :contains与:has都 ...
- Android Binder机制彻底梳理一
Binder架构图: 先来瞅一下它的整体架构图: 其中粉红部分是上层的Binder,而蓝色的则是下层的Binder,很显然上层的是依赖于下层的. 什么是Binder[有个大概了解]? 这里从几个层面来 ...
- Nastya Hasn't Written a Legend(Codeforces Round #546 (Div. 2)E+线段树)
题目链接 传送门 题面 题意 给你一个\(a\)数组和一个\(k\)数组,进行\(q\)次操作,操作分为两种: 将\(a_i\)增加\(x\),此时如果\(a_{i+1}<a_i+k_i\),那 ...
- git免密
免账号密码输入 git clone https://lichuanfa%40gitcloud.com.cn:lcf13870752164@git.c.citic/Citic-Data/bigdata_ ...
- 读取yaml文件小方法
def read_inf(inf_path): '''读取指定路径配置文件''' try: import yaml fr = open(inf_path) fy = yaml.load(fr) fr. ...
- Spark API--Spark 分区
一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...
- 基于Helm和Operator的K8S应用管理
https://blog.csdn.net/RancherLabs/article/details/79483013 大家好,今天我们分享的内容是基于Helm和Operator的K8S应用管理. 我们 ...
- 文件操作时:xreadlines和readlines的区别?
二者使用时相同,但返回类型不同,xreadlines返回的是一个生成器,readlines返回的是list
- Stability Analysis of Algorithms
算法(Algorithm)是指用来操作数据.解决程序问题的一组方法.对于同一个问题,使用不同的算法,也许最终得到的结果是一样的,比如排序就有前面的十大经典排序和几种奇葩排序,虽然结果相同,但在过程中消 ...
- NTSTATUS代码摘录
00000000 STATUS_SUCCESS00000000 STATUS_WAIT_000000001 STATUS_WAIT_100000002 STATUS_WAIT_200000003 ST ...