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 ...
随机推荐
- CentOS6.8 yum 安装 mysql5.7.12 完美步骤
一,wget http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm 二,yum localinstall mysql5 ...
- 前端调试利器——BrowserSync
此处记录一下踩过的坑 之前看的这个地址:http://www.browsersync.cn/ 也就是 BrowserSync的官网上面关于代理服务器的例子不管怎么试都不行 请看下例子 browser- ...
- HTML5游戏开发系列教程7(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-7/ 今天我们将完成我们第一个完整的游戏--打砖块.这次教程中,将 ...
- k8s-安装coreos+kubernetes
开始 软件 版本 分支 简称 Container Linux 1465.7.0 stable coreos kubernetes 1.7.3 stable k8s 本文主要内容来自coreos.com ...
- Django REST Framework 学习笔记
前言: 基于一些不错的RESTful开发组件,可以快速的开发出不错的RESTful API,但如果不了解开发规范的.健壮的RESTful API的基本面,即便优秀的RESTful开发组件摆在面前,也无 ...
- docker for spark
项目需求,有一个spark-streaming的程序,读kafka的数据,需要构建一个不使用hadoop的spark 以下建立的镜像参考网络,可以稍加修改就可以使用不同的版本. 可单独启动master ...
- JQuery EasyUI 扩展方法 日期控件 设置时间段函数
/** Jquery扩展方法--by hgx 2018年1月8日-- * 设置时间段函数,开始时间(1号)与结束时间(当前日期) * 传入参数:--spaceMonth:查询间隔月,1为间隔查询一个月 ...
- 写入Csv
//定义文件输出流 FILE *f; f = fopen("a.csv" , "wb"); fprintf(f,"aaa,23,sdf\n" ...
- BestCoder #58 div1
2015-10-08 19:14:54 总结:赛后补的一场.题目蛮有意思的. A:DFS 思路:搜一下几个环然后判断一下即可. #include <cstdio> #include < ...
- TOSCA自动测试工具跟QTP 和 Selenium的简单对比
1. 一个课程里的,可以做个简单的参考,有些地方不是很准确