LeetCode: Set Matrix Zeroes 解题报告
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 to show follow up.
Follow up:
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?

SOLUTION 1:
题目要求O(1)的空间消耗。
1. 我们可以使用第一行,第一列来作为Flag,记录某一行,某一列是否应该被设置为0.
2. 因为第一行,第一列共用左上角的flag,所以我们需要另外找2个flag来定义第一行,第一列本身是否应该设置为0.
row1Zero, col1Zero
3. 先扫描首行,首列把首行首列的flag算出。
4. 扫描其他的矩阵,将第一行每一列的flag算出。
5. 设置矩阵中除了首行首列的cells.
6. 设置首行,设置首列。
 public class Solution {
     public void setZeroes(int[][] matrix) {
         if (matrix == null || matrix.length ==
             || matrix[].length == ) {
             return;
         }
         boolean row1Zero = false;
         boolean col1Zero = false;
         int rows = matrix.length;
         int cols = matrix[].length;
         // Determine if the first column should be Zero.
         for (int i = ; i < rows; i++) {
             if (matrix[i][] == ) {
                 col1Zero = true;
                 break;
             }
         }
         // Determine if the first row should be Zero.
         for (int i = ; i < cols; i++) {
             if (matrix[][i] == ) {
                 row1Zero = true;
                 break;
             }
         }
         // we use the first row and the first col as the flag to record the
         // cells whether or not set to 0.
         for (int i = ; i < rows; i++) {
             for (int j = ; j < cols; j++) {
                 // 注意了,这个矩阵是0和非0,并不是0和1.
                 if (matrix[i][j] == ) {
                     // set the flag in the first line and the first column
                     matrix[i][] = ;
                     matrix[][j] = ;
                 }
             }
         }
         // set the inner cells.
         // Be careful: i, j start from 1.
         for (int i = ; i < rows; i++) {
             for (int j = ; j < cols; j++) {
                 if (matrix[i][] ==
                    || matrix[][j] == ) {
                     matrix[i][j] = ;
                 }
             }
         }
         // set the first row.
         if (row1Zero) {
             for (int i = ; i < cols; i++) {
                 matrix[][i] = ;
             }
         }
         // set the first col.
         if (col1Zero) {
             for (int i = ; i < rows; i++) {
                 matrix[i][] = ;
             }
         }
         return;
     }
 }
2014.1230 redo:
把前面三个循环合并,会简单一点儿。
 public class Solution {
     public void setZeroes(int[][] matrix) {
         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
             return;
         }
         boolean row1 = false;
         boolean col1 = false;
         int rows = matrix.length;
         int cols = matrix[0].length;
         // set flags.
         for (int i = 0; i < rows; i++) {
             for (int j = 0; j < cols; j++) {
                 if (matrix[i][j] != 0) {
                     continue;
                 }
                 // set the flag of a column and a row.
                 matrix[0][j] = 0;
                 matrix[i][0] = 0;
                 //  get flag of first row.
                 if (i == 0) {
                     row1 = true;
                 }
                 // get flag of first column.
                 if (j == 0) {
                     col1 = true;
                 }
             }
         }
         // set the matrix.
         for (int i = 1; i < rows; i++) {
             for (int j = 1; j < cols; j++) {
                 if (matrix[0][j] == 0 || matrix[i][0] == 0) {
                     matrix[i][j] = 0;
                 }
             }
         }
         // set first column
         if (col1) {
             for (int i = 0; i < rows; i++) {
                 // bug 1: can't use matrix[i][j]
                 matrix[i][0] = 0;
             }
         }
         // set first row.
         if (row1) {
             for (int i = 0; i < cols; i++) {
                 matrix[0][i] = 0;
             }
         }
     }
 }
GitHub Code:
LeetCode: Set Matrix Zeroes 解题报告的更多相关文章
- LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题
		Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ... 
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ... 
- 【LeetCode】Set Matrix Zeroes 解题报告
		今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ... 
- LeetCode 283 Move Zeroes 解题报告
		题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ... 
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
		[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ... 
- LeetCode 1 Two Sum 解题报告
		LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ... 
- 【LeetCode】Permutations II 解题报告
		[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ... 
- 【LeetCode】Island Perimeter 解题报告
		[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ... 
- 【LeetCode】Largest Number 解题报告
		[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ... 
随机推荐
- 关于DES加密中的 DESede/CBC/PKCS5Padding
			今天看到一段3DES加密算法的代码,用的参数是DESede/CBC/PKCS5Padding,感觉比较陌生,于是学习了一下. 遇到的java代码如下: Cipher cipher=Cipher.get ... 
- springmvc自己定义拦截器
			Spring MVC也能够使用拦截器对请求进行拦截处理,用户能够自己定义拦截器来实现特定的功能,自己定义的拦截器必须实现HandlerInterceptor接口. 直接看下样例: package co ... 
- SQL Union作用
			动态构造一个SQL语句然后执行,构造动态语句的查询语句如下 SELECT REPLACE(WMSYS.WM_CONCAT(STR),',',' UNION ') FROM (SELECT 'SELEC ... 
- Tomcat 监控方法
			Tomcat 监控方法 方法1:.使用tomcat自带的status页 具体方法: 步骤1:修改%tomcat安装路径%\conf \tomcat-users文件,配置admin设置权限.在<t ... 
- 基于.net 的加载自定义配置-误操作
			有时候 需要 将程序加载自定义的配置文件,除了自己写解析xml文件.内置的ConfigutionManager对象 是个不错的选项. 按照 app.config 的方式,做一个副本.然后从你的配置文件 ... 
- Linux查看GPU信息和使用情况
			Linux查看显卡信息: lspci | grep -i vga 使用nvidia GPU可以: lspci | grep -i nvidia [root@gpu-server-002 ~]# lsp ... 
- kubernetes 无法删除 pod 问题的解决
			[摘要] kubernetes 可能会产生垃圾或者僵尸pod,在删除rc的时候,相应的pod没有被删除,手动删除pod后会自动重新创建,这时一般需要先删除掉相关联的resources,实际中还要具体情 ... 
- try语句...
			#include<stdio.h>#include<iostream>using namespace std; int main( ){ try { throw "嗨 ... 
- Chrome浏览器桌面通知提示功能使用
			http://www.cnblogs.com/meteoric_cry/archive/2012/03/31/2426256.html 
- C# -- 等待异步操作执行完成的方式  C# -- 使用委托 delegate 执行异步操作  JavaScript -- 原型:prototype的使用  DBHelper类连接数据库  MVC View中获取action、controller、area名称、参数
			C# -- 等待异步操作执行完成的方式 C# -- 等待异步操作执行完成的方式 1. 等待异步操作的完成,代码实现: class Program { static void Main(string[] ... 
