【数组】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. 解题思路: ...
随机推荐
- java中的实例化
java中的new用于实例化一个对象 T1 a= new T1(); T2 b= new T1(); 区别: 问题1:不是实例化一个a,是实例化一个T1 T1 的一个 对象的引用 a 指向了堆空间里的 ...
- 2017 pycharm 激活码
BIG3CLIK6F-eyJsaWNlbnNlSWQiOiJCSUczQ0xJSzZGIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNzaWduZWVOYW1lIjoiI ...
- html使用技巧
line-height: 27px; /* lineheight和height保持一致就能达到文本垂直居中*/ .top_content li { list-style-image: url(../ ...
- 构建Maven项目自动下载jar包
使用Maven 自动下载jar包 右键单击项目,将项目 转换成Maven 项目 然后进去Maven官网 http://mvnrepository.com/ 这里有大量的jar包供我们使用,比如我现在要 ...
- Before an Exam
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93241#problem/B (654123) http://codeforces.com ...
- 关于BufferefReader.readLine()方法的理解
有以下代码: BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in)); String ...
- 提高Android和iOS调试编译速度
http://www.cnblogs.com/findumars/p/7841252.html 提高Android和iOS调试编译速度 如果您使用Delphi开发App,就会遇到:Android和 ...
- Android-上下文菜单Menu
上一篇博客介绍了,Android-普通菜单Menu,而这篇博客介绍Android-上下文菜单Menu AndroidManifest.xml 中加入权限: <!-- 读取联系人数据的权限 --& ...
- [Linux]几个armhf的ubuntu源
摘自百度贴吧,留存 http://mirrors.ustc.edu.cn/ubuntu-ports/http://ftp.ubuntu-tw.org/mirror/ubuntu-ports/http: ...
- LinqToHubble介绍及简单使用步骤——LinqToHubble是对HubbleDotnet的封装
或许你还你知道HubbleDotnet,下面简单对HubbleDotnet坐下介绍. HubbleDotNet是由盘古分词作者——eaglet 开发的一个基于.net framework 的开源免费的 ...