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.
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?
public class Solution {
public void setZeroes(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
int row = 0, col = 0;
boolean sig = true;
for(int i = 0; i < matrix.length; i ++){
for(int j = 0; j < matrix[0].length; j ++){
if(matrix[i][j] == 0){
if(sig){
row = i;
col = j;
sig = false;
} else {
matrix[row][j] = 0;
matrix[i][col] = 0;
}
}
}
}
if(sig) return;
for(int i = 0; i < matrix.length; i ++){
for(int j = 0; j < matrix[0].length; j ++){
if((matrix[row][j] == 0 || matrix[i][col] == 0) && i != row && j != col) matrix[i][j] = 0;
}
}
for(int i = 0; i < matrix.length; i ++){
matrix[i][col] = 0;
}
for(int j = 0; j < matrix[0].length; j ++){
matrix[row][j] = 0;
}
}
}
Set Matrix Zeroes的更多相关文章
- 55. Set Matrix Zeroes
Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- Leetcode 细节实现 Set Matrix Zeroes
Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...
- 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】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 ...
- 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(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- Set Matrix Zeroes leetcode java
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...
- LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fol ...
- [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 ...
随机推荐
- 安装Mongodb3.0.6单实例
[root@b28-17-51 ~]#mkdir -p /export/data [root@b28-17-51 ~]#mkdir -p /export/log [root@b28-17-51 exp ...
- linux下更改文件夹所属用户和用户组
改变所属用户组:chgrp -R users filename -R是为了递归改变文件夹下的文件和文件夹,users是要改为的用户组名称,filename是要改变的文件夹名称 ============ ...
- 6月24日AppCan移动开发者大会礼品清单遭泄露
6月24日,第一届AppCan移动开发者大会将在北京国际会议中心举办,大会以”平台之上,应用无限”为主题,全景展现移动应用发展趋势.AppCan 移动技术蓝图及80万开发者的技术实践成果. 大会现场礼 ...
- Binder相关
Binder分析 需了解C/S还有管理这些的ServiceManager. 简单来讲你(client)有个包裹(parcel)让快递员(proxy)送到公司(server), 公司(server) ...
- poj 1862 Stripies/优先队列
原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: #inc ...
- go语言示例-Timer计时器的用法
计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...
- 历时八年,HTML5 标准终于完工了
万维网联盟(W3C)2014年10月29日泪流满面地宣布,经过几乎8年的艰辛努力,HTML5标准规范终于最终制定完成了,并已公开发布. 在此之前的几年时间里,已经有很多开发者陆续使用了HTML5的部分 ...
- 在DNS管理器——用局域网IP指定你所起的域名名称
在服务器上面,进行以下相关的操作: 第一步:打开DNS管理器; 第二步:在“正向查找区域”添加域名为:icanyin.net; 第三步:添加主机pm,将域名pm.icanyin.net解析为IP地址: ...
- Mono for Android (3)-- AbsoluteLayout、FrameLayout、LinearLayout、RelativeLayout、TableLayout
AbsoluteLayout:允许开发人员将视图放在所定义的位置.该布局已经过时了,建议改用其他 FrameLayout:最简单的布局选项,其设计目的是在屏幕上显示单个对象.所有元素都固定在左上角.如 ...
- windows多线程编程(一)(转)
源出处:http://www.cnblogs.com/TenosDoIt/archive/2013/04/15/3022036.html CreateThread:Windows的API函数(SDK函 ...