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 ... 
随机推荐
- 3.css中的颜色
			css中颜色的设置形式主要有三种方式:颜色名称.十六进制代码和十进制代码. 在古老的 HTML4 时,颜色名称只有 16 种. 颜色名称 十六进制代码 十进制代码 含义 black #000000 ... 
- python 装饰器(decorator)
			装饰器(decorator) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 装饰器(decorator)是一种高级Python语 ... 
- psql: 致命错误:  用户 "postgres" Ident 认证失败
			RedHat: 问题: psql -U postgres 时出现:psql: 致命错误: 用户 "postgres" Ident 认证失败 解决: 修改 /var/lib/pgs ... 
- SegmentFault 2014黑客马拉松 北京 作品demo
			1号作品展示——最熟悉的陌生人 app 利用录音(声纹识别)和照片来让好久不见的见面变得不那么尴尬. 2号作品展示——神奇魔镜 app 灵感来自通话<白雪公主>,穿越到今天的“魔镜”功能依 ... 
- 浅谈objective—C管理内存
			这段时间被导师催着论文,调试各种BUg,也是醉了,发现很大程度上,内存出错,栈溢出,各种悲剧.那么今天就和大家一起对OC的内存管理来个探微吧.Objective-C使用一个保留计数记录了我们所创建的所 ... 
- Android 设计模式
			简介 项目开发中发现问题.解决问题这个过程中会出现很多问题,比如重复出现.某个问题的遗留,这些问题的本质就是设计模式.今天记录设计模式的知识点. 内容 在java以及其他的面向对象设计模式中,类与类之 ... 
- ios中怎么样自动剪切图片周围超出的部分
			UIImageView *image = [[UIImageView alloc] init]; image.clipsToBounds = YES; 
- 不同系统间传输float型数据
			#include <stdio.h> #include <string.h> int main(void) { union result { float d; ... 
- Filter介绍
			Filter可人为是Servlet的一种“加强版”,它重要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链.使用Filter的完整的流程是:Filt ... 
- [shell基础]——sort命令
			sort命令 sort是按照ASCII码升序输出,且是从首字符依次向后比较的 常见选项 -c 测试文件是否已经被排序 -r 逆向排序 -n 按照数字数值大小排序 -t 指定分割 ... 
