题目:矩阵置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)的更多相关文章

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

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

  3. Leetcode73. Set Matrix Zeroes矩阵置零

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

  4. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

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

  6. 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 ...

  7. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  8. 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 ...

  9. LeetCode第[78]题(Java):Subsets(求子集)扩展——第[90]题:Subsets 2

    题目:矩阵置0 难度:Easy 题目内容:   Given a set of distinct integers, nums, return all possible subsets (the pow ...

随机推荐

  1. Win2003X64位,IIS6.0 32位 浏览报错的解决方案

    目录 问题案例 原因分析 解决问题 其他 问题案例 1)服务浏览出现: service unavailable 2)服务浏览出现:HTTP 404 当前页找不到 3)在事件查看器:应用程序中报错:在同 ...

  2. 使用QJM构建HDFS HA架构(2.2+)

    转载自:http://blog.csdn.net/a822631129/article/details/51313145 本文主要介绍HDFS HA特性,以及如何使用QJM(Quorum Journa ...

  3. 模块讲解----json模块(跨平台的序列化与反序列化)

    一.json的特点 1.只能处理简单的可序列化的对象:(字典,列表,元祖) 2.json支持不同语言之间的数据交互:(python  -  go,python - java)   二.使用场景 1.玩 ...

  4. (转)VS中的路径宏 vc++中OutDir、ProjectDir、SolutionDir各种路径说明

      $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的值.有关更多信息,请参见更改用于 C/C++ 调试配置的项目设置. $(References) 以分号分隔的引用列表被添 ...

  5. yii2美化url

    http://blog.csdn.net/xundh/article/details/45418265

  6. Django-RestFrameWork之分页 视图 路由 渲染器

    目录 一.分页 二.视图 三.路由 四.渲染器 一.分页 试问如果当数据量特别大的时候,你是怎么解决分页的? 方式a.记录当前访问页数的数据id 方式b.最多显示120页等 方式c.只显示上一页,下一 ...

  7. PHP压缩与解压Zip(PHPZip类)

    <?php     class PHPZip     {         private $ctrl_dir     = array();         private $datasec    ...

  8. hihocoder1477 闰秒

    地址:http://hihocoder.com/problemset/problem/1477 题目: 闰秒 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 计算机系统中 ...

  9. android线程学习心得

    有一篇关于android线程讲的非常好,大家可以参考下,其中有一句话讲的非常好,就拿来做开篇之句: 当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主 ...

  10. BestCoder #58 div1

    2015-10-08 19:14:54 总结:赛后补的一场.题目蛮有意思的. A:DFS 思路:搜一下几个环然后判断一下即可. #include <cstdio> #include < ...