LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)
题目:矩阵置0
难度:Easy
题目内容:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
翻译:
给定一个m x n矩阵,如果一个元素是0,就把它的整行和列设为0。
要求:就地置0。
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]
]
我的思路:因为要求就地,所以不能新建一个矩阵,然后遇见0就将所在行列全部置0
所以就全部循环一遍,将所有的0的行列下标用List<Integer[]>记录下来,0号位为行,1号位为列。
然后再循环此List,分别调用置零方法即可。
我的代码:
public void setZeroes(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return;
List<Integer[]> list = new ArrayList<Integer[]>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
list.add(new Integer[]{i,j});
}
}
}
for (Integer[] x : list) {
setZero(matrix, x[0], x[1]);
}
} public void setZero(int[][] matrix, int i, int j) {
for (int n = 0; n < matrix.length; n++) {
matrix[n][j] = 0;
}
for (int m = 0; m < matrix[0].length; m++) {
matrix[i][m] = 0;
}
}
我的复杂度:O(m*n)+ O((m+n)*x) x为矩阵中0的个数
编程过程中的问题:
1、无
(代码中的Integer[]可以使用int[]替换)
答案代码:
public void setZeroes(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return;
boolean row = false,col = 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) row = true;
if (j == 0) col = true;
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < matrix.length; i++) { // 注意此处应该是从1开始,因为第【0,0】如果为0也进行判断的话就会把第0列(标志列)全部设置为0,则接下来整个矩阵都会为0
if (matrix[i][0] == 0) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1; j < matrix[0].length; j++) { //
if (matrix[0][j] == 0) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][j] = 0;
}
}
}
if (row) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if (col) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
}
答案复杂度:O(m*n)
答案思路:利用第0行和第0列作为“标志位”,并设置两个boolean变量记录“标志位”是否需要置0,这种方法相对于我的方法在最后置0的时候避免了重复置0的动作,原矩阵再多的0,最也只需要O(m*n)次置0
LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)的更多相关文章
- 【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] 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] ] 输 ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2
题目:矩阵置0 难度:Easy 题目内容: Given a set of distinct integers, nums, return all possible subsets (the pow ...
随机推荐
- mysql union (all) 后order by的排序失效问题解决
上sql select * FROM ( SELECT SUM(c.overtime_num) AS delay_num, ) rate , '全网' as reaCodeFROM calc_vmap ...
- 009-Shell 函数
一.函数定义 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式如下: [ function ] funname [()] { action; ...
- Spring源码解析(一)开篇
前言 Spring源码继承结构比较复杂,看过以后经常会忘记.因此,记录一下源码分析的过程,方便以后回顾.本次分析的Spring源码版本为3.2.15. 另外,一提Spring就是IOC.DI等等,我们 ...
- Spark2.0 协同过滤推荐
ALS矩阵分解 http://blog.csdn.net/oucpowerman/article/details/49847979 http://www.open-open.com/lib/view/ ...
- LDA算法里面Dirichlet分布的两个参数alpha和beta怎样确定?
本文参考自:https://www.zhihu.com/question/21692336/answer/19387415 方法一: alpha 是 选择为 50/ k, 其中k是你选择的topi ...
- Java并发编程实战3章
1.同步包括两方面:原子性和可见性. 2.可见性:因为在多线程程序中,如果没有采用正确的同步,有些线程就会得到失效数据. Java内存模型要求,变量的读取操作和写入操作都必须是原子操作,但对于非vol ...
- zen-cart安装出现时区错误解决办法
有时候在安装zen-cart的时候出现时区错误,提示: ERROR: date.timezone not set in php.ini. Please contact your hosting com ...
- MySQL临时表的简单用法(复制)
当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后在这些表运行查 ...
- web.xml中配置spring配置(application.xml)文件
application.xml 一般放到WEB-INF下,当然,你也可以将它放到任意问题,但需要web.xml指向到该文件 1.application.xml配置 <?xml version=& ...
- 你知道C语言为什么会有“_”(下划线)吗?
学过C语言的都知道,变量名只能由字母.数字.下划线组成,且只能以字母或者下划线开头. 学英语时我们都学过连字符(“-”),这个东东主要用来连接单词的.那么C语言为什么不直接 用连字符,而要改用下划线呢 ...