Algorithms—73.Set Matrix Zeroes
来源:互联网 发布:jdk源码怎么看 编辑:程序博客网 时间:2024/06/05 20:48
思路:首先是O(m+n)空间复杂度的方法,新开辟空间,然后遍历所有的点,如果有那个点的值为0,则将其横纵坐标的值记录进去。然后是空间复杂度为常数的。即把这些横纵坐标记录在原数组中,改为0即可。但是需要注意的是2点,1.在查询前先判断(x,0)和(0,y)两条线上是否存在0的点。2.重新遍历赋值的时候值修改除上述2条线的内部结构。3.再根据1中查询的结果决定是否修改这2条线。
public class Solution { public void setZeroes(int[][] matrix) { boolean f=true; for (int i = 0; i < matrix.length; i++) {if (matrix[i][0]==0) {f=false;break;}} boolean l=true; for (int i = 0; i < matrix[0].length; i++) {if (matrix[0][i]==0) {l=false;}} for (int i = 0; i < matrix.length; i++) {for (int j = 0; j < matrix[i].length; j++) {if (matrix[i][j]==0) {matrix[i][0]=0;matrix[0][j]=0;}}} for (int i = 1; i < matrix.length; i++) {for (int j = 1; j < matrix[i].length; j++) {if (matrix[i][0]==0||matrix[0][j]==0) {matrix[i][j]=0;}}} if (!f) {for (int i = 0; i < matrix.length; i++) {matrix[i][0]=0;}} if (!l) { for (int i = 0; i < matrix[0].length; i++) {matrix[0][i]=0;}} }}
耗时:396ms,中游。
0 0
- Algorithms—73.Set Matrix Zeroes
- 73.Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 73. Set Matrix Zeroes
- 76Minimum Window Substring
- 熵
- 数字图像处理中相关和卷积的区别
- 匹配不包含指定中文字符的行
- extJs常用的四种Ajax异步提交
- Algorithms—73.Set Matrix Zeroes
- git 工作笔记(自己常用的 git 命令记录)
- Android属性动画PropertyAnimation系列三之LayoutTransition(布局容器动画)
- Uva10795 - A Different Task
- 推荐!手把手教你使用Git
- 毕业初入职
- adaBoost
- static
- 类的前向声明与调用