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.
public class Solution {
public void setZeroes(int[][] matrix) {
if(matrix==null){
return;
}
int m=matrix.length;
int n=matrix[0].length;
List<Integer> r=new ArrayList<Integer>();
List<Integer> c=new ArrayList<Integer>();
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(matrix[i][j]==0){
r.add(i);
c.add(j);
}
}
}
for(int i=0; i<r.size(); i++){
int row=r.get(i);
int column=c.get(i);
for(int k=0; k<n; k++){
matrix[row][k]=0;
}
for(int l=0; l<m; l++){
matrix[l][column]=0;
}
}
}
}
二刷:
题目说明不能用额外空间,为了减少重复计算, 我们只是把等于零的相对应的头row和头column标为零。
注意第一排和列共用一个matrix[0][0], 应该分开讨论:
class Solution {
public void setZeroes(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return;
}
int row = matrix.length;
int column = matrix[0].length;
//Check first column
boolean isFirstCol = false;
for(int i = 0; i < row; i++){
if (matrix[i][0] == 0){
isFirstCol = true;
}
for(int j = 1; j < column; j++){
if(matrix[i][j] == 0){
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for(int i = 1; i< row; i++){
for(int j = 1; j < column; j++){
if(matrix[i][0] == 0 || matrix[0][j] == 0){
matrix[i][j] = 0;
}
}
}
//check first row
if(matrix[0][0] == 0){
for(int j = 0; j< column; j++){
matrix[0][j] = 0;
}
}
//check first column
if(isFirstCol){
for(int i =0; i<row; i++){
matrix[i][0] = 0;
}
}
}
}
LeetCode-Set Matrix Zeroes的更多相关文章
- LeetCode: Set Matrix Zeroes 解题报告
Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...
- [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 ...
- [leetcode]Set Matrix Zeroes @ Python
原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...
- LeetCode OJ--Set Matrix Zeroes **
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...
- 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. 原题链接:h ...
- LeetCode Set Matrix Zeroes(技巧+逻辑)
题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...
- [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 ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- Leetcode 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
随机推荐
- spring入门教程——笔记
Spring学习笔记(1)----简单的实例 --------------------------------- 首先需要准备Spring包,可从官方网站上下载. 下载解压后,必须的两个包是s ...
- 数据类型int、bigint、smallint 和 tinyint范围
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. int ...
- 关于spring中无法将service注入到servlet中的问题
首先,servlet是动态网页项目区别于普通的java项目的,是动态网页项目中web.xml主要配置文件管理的,而spring只能管理普通的pojo,而没办法直接注入,尽管你的注入方式和配置方式都没有 ...
- iOS-打电话、发短信、发邮件、打开浏览器
- (IBAction)showAlert:(UIButton *)sender { NSString *phoneNumber=@"18500138888"; NSString ...
- 2016 - 1 - 27 javaScrip初步(一)
<head> </head> <body> <!-- The onclick attribute is the code that happens when ...
- Rails学习笔记二
rails框架: MVC模型:模型.视图.控制器 来自网站的请求先是被传递到路由,判断请求找出控制器中的方法,处理请求,控制器中的方法也被称作行为 行为可以与模型交互,访问数据库,并为视图准备信息 由 ...
- 将string转换成char* (转)
原文:http://blog.sina.com.cn/s/blog_786ce14d01014lpr.html string 是c++标准库里面其中一个,封装了对字符串的操作把string转换为cha ...
- LayaAir引擎——(九)
var h = new Array(); var j = new Array(); var xbCursor = 0; function xbinit() { xbinitName(); xbRect ...
- C#通过WatiN操作页面中内嵌的Iframe
通过WatiN.Core.Broswer.Frame()方法来获取iframe对象,之后的容器就是frame,然后进行操作. 下面的例子是登录QQ空间的: Frame frame = browser. ...
- Django1.3 创建项目
经历了各种失败各种烦恼以后Django开发环境终于搭建好了! 系统环境:ubuntu12.04 Django版本1.3.1 Python版本 2.7.3 接下来就兴建一个项目练习一下 1.创 ...