个人记录-LeetCode 73. Set Matrix Zeroes
2017-01-06 20:16
302 查看
问题:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
代码示例:
1、整体思路其实就是先标记后处理,使用的是O(m + n) space。
2、使用constant space,本质还是先标记后处理,不过是将标记信息写入到第一行和第一列。
节省了空间,但写法变得不够直观,从代码可读性来讲,刻意这么做没有什么意义。
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?
代码示例:
1、整体思路其实就是先标记后处理,使用的是O(m + n) space。
public class Solution { public void setZeroes(int[][] matrix) { if (matrix == null || matrix.length < 1 || matrix[0].length < 1) { return; } int rowNum = matrix.length; int colNum = matrix[0].length; Set<Integer> rowSet = new HashSet<>(); Set<Integer> colSet = new HashSet<>(); //记录将被清零的行和列 for (int i = 0; i < rowNum; ++i) { for (int j = 0; j < colNum; ++j) { if (matrix[i][j] == 0) { rowSet.add(i); colSet.add(j); } } } //执行清零操作 for (int i = 0; i < rowNum; ++i) { if (rowSet.contains(i)) { for (int j = 0; j < colNum; ++j) { matrix[i][j] = 0; } } } for (int j = 0; j < colNum; ++j) { if (colSet.contains(j)) { for (int i = 0; i < rowNum; ++i) { matrix[i][j] = 0; } } } } }
2、使用constant space,本质还是先标记后处理,不过是将标记信息写入到第一行和第一列。
public class Solution { public void setZeroes(int[][] matrix) { if (matrix == null || matrix.length < 1 || matrix[0].length < 1) { return; } int rowNum = matrix.length; int colNum = matrix[0].length; //先单独标记第一列 boolean firstColHasZero = false; for (int i = 0; i < rowNum; ++i) { if (matrix[i][0] == 0) { firstColHasZero = true; break; } } //先单独标记第一行 boolean firstRowHasZero = false; for (int j = 0; j < colNum; ++j) { if (matrix[0][j] == 0) { firstRowHasZero = true; break; } } //其它标记写入第一行和第一列 for (int i = 1; i < rowNum; ++i) { for (int j = 1; j < colNum; ++j) { if (matrix[i][j] == 0) { matrix[0][j] = 0; matrix[i][0] = 0; } } } //处理普通的行列 for (int i = 1; i < rowNum; ++i) { if (matrix[i][0] == 0) { for (int j = 0; j < colNum; ++j) { matrix[i][j] = 0; } } } for (int j = 1; j < colNum; ++j) { if (matrix[0][j] == 0) { for (int i = 0; i < rowNum; ++i) { matrix[i][j] = 0; } } } //最后单独处理第一行和第一列 if (firstRowHasZero) { for (int j = 0; j < colNum; ++j) { matrix[0][j] = 0; } } if (firstColHasZero) { for (int i = 0; i < rowNum; ++i) { matrix[i][0] = 0; } } } }
节省了空间,但写法变得不够直观,从代码可读性来讲,刻意这么做没有什么意义。
相关文章推荐
- LCT模板
- anaconda libsvm安装32位或64位
- 【BZOJ1208】宠物收养所(splay)
- java的多线程
- 进程间通信方式有哪些?各自有哪些优缺点?
- 解决Cannot change version of project facet Dynamic web module to 2.5
- 制作wordpress 导航栏
- Android横向ListView
- 导入androidStudio项目出现错误
- CSS3常用属性及效果汇总
- 202. Happy Number 难度:easy
- Subsets
- 如何关闭MIUI新版本中加入的“system锁”校验!
- Github 工程README.md 添加图片
- eclipse中使用git
- android 超简单实现dialog全屏显示
- C#77课的主要内容
- R语言学习五
- JVM实用参数(八)GC日志
- C++笔记之引用