073 Set Matrix Zeroes 矩阵置零
给定一个 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 矩阵置零的更多相关文章
- [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 ...
- Leetcode73. Set Matrix Zeroes矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输 ...
- [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] 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 ...
- [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 矩阵置零 Python
矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1 ...
- LeetCode:矩阵置零【73】
LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], ...
- Java实现 LeetCode 73 矩阵置零
73. 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ...
- 【python】Leetcode每日一题-矩阵置零
[python]Leetcode每日一题-矩阵置零 [题目描述] 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 .请使用 原地 算法. 进阶: 一个直观的解 ...
随机推荐
- codeforces 705B B. Spider Man(组合游戏)
题目链接: B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- OpenCV——PS滤镜算法之Spherize 球面化(凸出效果)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- Android Studio的技巧
1.快速添加add unimplements methods: 右键generate 2.快速添加try-catch:左边就有一个小电灯,然后可以选. 3.格式化OPTION + CMD + L ( ...
- manacher(无讲解)
BZOJ3325: [Scoi2013]密码 https://lydsy.com/JudgeOnline/problem.php?id=3325 分析: 根据前i个字符和一些不等和相等条件就可以确定每 ...
- WPF Get Multibinding Expression, Update Source,
wpf 拿到某个control的multibinding以及其中每个Binding 1. 拿到multibinding MultiBindingExpression mbe = Bindin ...
- 关于UML图的生成
想把一个java工程生成UML图非常简单,之前我的eclipse是4.2.0的,没有对应的GEF,所以我索性就直接把工程粘到了My Eclipse中,因为My Eclipse里面有UML自动生成的功能 ...
- java面试题汇总(1)
1)Java 中能创建 volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组, 将会受到 v ...
- 【opencv学习笔记三】opencv3.4.0数据类型解释
opencv提供了多种基本数据类型,我们这里分析集中常见的类型.opencv的数据类型定义可以在D:\Program Files\opencv340\opencv\build\include\open ...
- UDP 编程 客服咨询回复
package 网络编程_客户咨询; import java.io.IOException; import java.net.DatagramPacket; import java.net.Datag ...
- [转]C/C++获取当前系统时间
原文转自:http://www.cnblogs.com/mfryf/archive/2012/02/13/2349360.html 个人觉得第二种还是比较实用的,而且也是最常用的~ 不过当计算算法耗时 ...