[Lintcode] Set Matrix Zeroes矩阵归零
来源:互联网 发布:java 线程间共享数据 编辑:程序博客网 时间:2024/05/17 23:55
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Example
Given a matrix
[ [1,2], [0,3]],
return
[
[0,2],
[0,0]
]
public class Solution { /** * @param matrix: A list of lists of integers * @return: Void */ public void setZeroes(int[][] matrix) { if(matrix.length == 0) return; if(matrix[0].length == 0) return; boolean row = false, col = false; if(matrix[0][0] == 0) { row = true; col = true; } else { for(int i = 0; i < matrix.length; i++) { if(matrix[i][0] == 0) { col = true; break; } } for(int i = 0; i < matrix[0].length; i++) { if(matrix[0][i] == 0) { row = true; break; } } } for(int i = 1; i < matrix.length; i++) { for(int j = 1; j < matrix[0].length; j++) { if(matrix[i][j] == 0) { matrix[i][0] = 0; matrix[0][j] = 0; } } } for(int i = 1; i < matrix.length; i++) { if(matrix[i][0] == 0) { for(int j = 1; j < matrix[0].length; j++) matrix[i][j] = 0; } else { if(matrix[0][0] == 0 || col) matrix[i][0] = 0; } } for(int i = 1; i < matrix[0].length; i++) { if(matrix[0][i] == 0) { for(int j = 1; j < matrix.length; j++) matrix[j][i] = 0; } else { if(matrix[0][0] == 0 || row) matrix[0][i] = 0; } } if(row || col) matrix[0][0] = 0; }}
0 0
- [Lintcode] Set Matrix Zeroes矩阵归零
- 162.Set Matrix Zeroes-矩阵归零(中等题)
- leetcode Set Matrix Zeroes矩阵设置零
- LintCode-矩阵归零
- LintCode: 矩阵归零
- 矩阵归零-LintCode
- lintcode-矩阵归零
- 【矩阵】Set Matrix Zeroes
- lintcode-矩阵归零-162
- LintCode 162-矩阵归零
- (每日算法)LeetCode--Set Matrix Zeroes (矩阵置零)
- LeetCode-73 Set Matrix Zeroes(设置零矩阵)
- Leetcode 73. Set Matrix Zeroes 矩阵置零 解题报告
- leetcode 73. Set Matrix Zeroes-矩阵置零|数组
- lintcode刷题——矩阵归零
- LeetCode2.1.20 @ Set Matrix Zeroes 矩阵部分元素置零 D3F5
- 【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】
- n*m 矩阵: set matrix zeroes
- sql注入原理
- CentOS更改开机启动顺序
- Android实现沉浸式状态栏
- 《丁小柒的JavaScript大神之路》----javascript函数
- 协议森林01 邮差与邮局 (网络协议概观)
- [Lintcode] Set Matrix Zeroes矩阵归零
- android MediaRecorder类
- python 命令行解析工具 argparse 基本使用
- 软考总结
- js作用域相关问题
- 21种设计模式之——单例模式
- 使用Base64编码对图片进行编码与显示
- Activity
- 1001. 害死人不偿命的(3n+1)猜想 (15)