给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零。
你有没有使用额外的空间?
使用 O(mn) 的空间不是一个好的解决方案。
使用 O(m + n) 的空间有所改善,但仍不是最好的解决方案。
你能设计一个使用恒定空间的解决方案吗?
详见:https://leetcode.com/problems/set-matrix-zeroes/description/

Java实现:

class Solution {
public void setZeroes(int[][] matrix) {
int m=matrix.length;
int n=matrix[0].length;
boolean rowZero=false;
boolean colZero=false;
for(int j=0;j<n;++j){
if(matrix[0][j]==0){
rowZero=true;
}
}
for(int i=0;i<m;++i){
if(matrix[i][0]==0){
colZero=true;
}
}
for(int i=1;i<m;++i){
for(int j=1;j<n;++j){
if(matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(int i=1;i<m;++i){
for(int j=1;j<n;++j){
if(matrix[i][0]==0||matrix[0][j]==0){
matrix[i][j]=0;
}
}
}
if(rowZero){
for(int j=0;j<n;++j){
matrix[0][j]=0;
}
}
if(colZero){
for(int i=0;i<m;++i){
matrix[i][0]=0;
}
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4341590.html

073 Set Matrix Zeroes 矩阵置零的更多相关文章

  1. [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 ...

  2. Leetcode73. Set Matrix Zeroes矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  3. [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 ...

  4. [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. Exampl ...

  5. [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 ...

  6. leetcode 73 矩阵置零 Python

    矩阵置零     给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1 ...

  7. LeetCode:矩阵置零【73】

    LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   ...

  8. Java实现 LeetCode 73 矩阵置零

    73. 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ...

  9. 【python】Leetcode每日一题-矩阵置零

    [python]Leetcode每日一题-矩阵置零 [题目描述] 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 .请使用 原地 算法. 进阶: 一个直观的解 ...

随机推荐

  1. memcached高可用

    http://sourceforge.net/projects/repcached/ memcached-1.2.8-repcached-2.2.tar.gz tar zxvf memcached-1 ...

  2. <%!%>声明的变量和在<%%>中声明的变量的区别

    通过写一个demo,查看Jsp_Servlet源码可知: <%!%>声明的变量是类似类的成员变量,<%%>中的变量是方法中的变量. 参考博客: http://www.cnblo ...

  3. 三:JMS消息服务规范

    一:JMS是什么?--->JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API--->用于在两个应用程 ...

  4. ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)

    Description   Problem B: Myacm Triangles Problem B: Myacm Triangles Source file: triangle.{c, cpp, j ...

  5. 关于java基础中,接口里面父类的对象指向子类的引用

    父类的引用指向子类的对象,它只能看的到父类的那些方法~ 子类自身的方法看不到-- ······························· 如: interface Singer { //定义了 ...

  6. Socket 阻塞与非阻塞模式

    http://blog.sina.com.cn/s/blog_5d0990c7010115ib.html

  7. C++ - main()函数参数

    main()函数及其参数说明 main()函数主要形式: int main(void) int main(int argc, char *argv[]) = int main(int argc, ch ...

  8. this解惑

    前言 要正确理解this,首先得理解执行上下文,这里推荐汤姆大叔的执行上下文,因为this是在运行代码时确认具体指向谁,箭头函数除外. 全局作用域中的this node: 每个javaScript文件 ...

  9. 352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  10. Codeforces Round #299 (Div. 2)【A,B,C】

    codeforces 535A-水题: #include <bits/stdc++.h> using namespace std; typedef long long LL; char s ...