【数组】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?
思路:
O(mn)解法:用m*n的空间记录每个某个位置是否要设置成0。
O(m+n)解法:用一个数组记录某一列是否需要设置成0,用一个数组记录某一行是否需要设置成0。
常数空间解法:如果我们可以用matrix本身的空间来记录,就不需要额外的空间了。用matrix第一行记录相应的列是都需要置0,第一列来记录相应的行是否需要置0。用来记录之前需要提前判断第一行和第一列是否需要置0。然后遍历matrix(出了第一行和第一列)如果matrix[i][j]等于0,那么matrixp[i][0] = 0 (行标志),matrix[0][j] = 0(列标志)。再次遍历matrix只要对应位置的行标志或者列标志等于0,相应的位置就置0,。最后还要看第一行和第一列是否需要置0。
/**
* @param {number[][]} matrix
* @return {void} Do not return anything, modify matrix in-place instead.
*/
var setZeroes = function(matrix) {
var m=matrix.length,n=matrix[0].length;
var firstC=1,firstR=1;
for(var i=0;i<m;i++){
if(matrix[i][0]==0){
firstC=0;
}
}
for(var i=0;i<n;i++){
if(matrix[0][i]==0){
firstR=0;
}
} for(var i=1;i<m;i++){
for(j=1;j<n;j++){
if(matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(var i=1;i<m;i++){
for(j=1;j<n;j++){
if(matrix[i][0]==0||matrix[0][j]==0){
matrix[i][j]=0;
}
}
} if(firstC==0){
for(var i=0;i<m;i++){
matrix[i][0]=0;
}
}
if(firstR==0){
for(var i=0;i<n;i++){
matrix[0][i]=0;
}
} };
【数组】Set Matrix Zeroes的更多相关文章
- 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 ...
- 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 ...
- 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 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(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- 【Leetcode】【Medium】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. 解题思路: ...
随机推荐
- Google Maps 基础
创建一个简单的 Google 地图 现在让我们创建一个简单的 Google 地图. 以下是显示了英国伦敦的 Google 地图: <!DOCTYPE html> <html> ...
- codeforces 702C Cellular Network 2016-10-15 18:19 104人阅读 评论(0) 收藏
C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- codevs 1083
这道题是看了人家大牛的解题报告: 对了,要说明一下,(A+B)&1 ,表示,判断(A+B)是奇数否? 下面给出代码: #include<iostream> #include< ...
- 加锁并发算法 vs 无锁并发算法
Heinz Kabutz 在上周举办了一次成功 JCrete研讨会,我在会上参加了对一种新的 StampedLock(于JSR166中 引入) 进行的评审.StampedLock (邮戳锁) 旨在解决 ...
- hdu 4957 贪心破木桶接水大trick
http://acm.hdu.edu.cn/showproblem.php?pid=4957 拿n只破的木桶去接水,每只木桶漏水速度为a[i],最后要得到b[i]单位的水,自来水的出水速度为V,木桶里 ...
- Oracle EBS 采购 接收入库 接口开发
http://blog.itpub.net/25164132/viewspace-746657/ 接收入库是项目中会经常碰到的开发,这类开发一般来说比较简单,但是接收入库在Oracle中其实涉及到很多 ...
- Android SharedPreference
在Android开发过程中,Android提供了SharedPreference共享首选项,它的用途就是,用于保存软件配置信息,APP使用过程中,需要用到的配置信息,例如:音量大小等: SharedP ...
- 设计模式之外观模式(Facade Pattern)
一.什么是外观模式? 简单的说,外观模式是用来简化接口的. 通常,我们觉得一个子系统不好用,可能是因为它提供的外部接口太接近低层组件,让我们用起来感到很麻烦. 因为我们不需要知道内部细节,我们只想要一 ...
- 对Cookie和Session的理解
本篇文章系自己总结经验,如果有朋友感觉哪里有问题,欢迎留言评论,谢谢~! Cookie和Session的产生背景: 在动态页面里面,每个变量都是有有效期的,所有的变量的最大生命周期就是一个脚本的周期( ...
- C#字符串转UNICODE
public static string StringToUnicode(string s)//字符串转UNICODE代码 { char[] charbuffers = s.ToCharArray() ...