今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错!

【题目】

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?

【思路】

我直接看了以下的要求,最先想到的就是O(m+n)的空间,就是加一行一列来标记哪行哪列有0;反而是O(mn)的空间不知该怎么利用了。

接下来想O(1)空间,略微一想,居然想到了答案,就是相比用O(m+n)的空间,不额外加一行一列,就用第一行和第一列来存储哪行哪列有0。当然,这样做比較麻烦的是第一行第一列的原始信息,须要先保存下来。

前两次写的时候,因为第一行第一列没有处理好,导致WA。

AC后看网上答案,看到其它人也是这么一个思路,挺高兴的。

【Java代码,O(1)空间】

public class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int i, j; //先标记第一行和第一列是否有0
boolean firstRow = false, firstCol = false;
for (j = 0; j < n; j++) {
if (0 == matrix[0][j]) {
firstRow = true;
break;
}
}
for (i = 0; i < m; i++) {
if (0 == matrix[i][0]) {
firstCol = true;
break;
}
} //从第二行第二列还是遍历,假设遇到0,则把它所在行和列的第一个值设为0
for (i = 1; i < m; i++) {
for (j = 1; j < n; j++) {
if (0 == matrix[i][j]) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
} //把第一列的0所在行都设为0,把第一行的0所在列都设为0
for (i = 1; i < m; i++) {
if (0 == matrix[i][0]) {
for (j = 1; j < n; j++) {
matrix[i][j] = 0;
}
}
}
for (j = 1; j < n; j++) {
if (0 == matrix[0][j]) {
for (i = 1; i < m; i++) {
matrix[i][j] = 0;
}
}
} //依据标记决定第一行和第一列是否全设为0
if (firstRow) {
for (j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
if (firstCol) {
for (i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
}
}

【LeetCode】Set Matrix Zeroes 解题报告的更多相关文章

  1. 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 ...

  2. LeetCode: Spiral Matrix II 解题报告-三种方法解决旋转矩阵问题

    Spiral Matrix IIGiven an integer n, generate a square matrix filled with elements from 1 to n2 in sp ...

  3. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  4. 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 ...

  5. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

随机推荐

  1. nyoj 7 街区最短路径问题 【数学】

    找出横纵坐标的中位数,怎么找:先对x排序找x的中位数x0,再对y排序找y的中位数y0:最后统计各点到中位数点(x0, y0)的总距离: 街区最短路径问题 时间限制:3000 ms  |  内存限制:6 ...

  2. [原创].NET 分布式架构开发实战五 Framework改进篇

    原文:[原创].NET 分布式架构开发实战五 Framework改进篇 .NET 分布式架构开发实战五 Framework改进篇 前言:本来打算这篇文章来写DAL的重构的,现在计划有点改变.之前的文章 ...

  3. pyspark简要原则

    概要 这是一个看前一段时间spark的python支持的时,有点简单的后pyspark内python代码,我们把一个一般流程.虽然几乎没有python,但基本上能看懂pyspark它是如何使不同的虚拟 ...

  4. Golang+Mongodb

    Golang+Mongodb打造你的第一个站点 很多人推荐MEAN来开发站点.MEAN就是M:mongodb,E:expressjs.A:angular最后的N:nodejs. 但是如果你亲身的体会到 ...

  5. 初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)

    笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/31824179 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...

  6. php学习之路:WSDL详细解释(两)

    3.定义服务使用的逻辑消息 当服务的操作被调用时.服务被定义为消息交换.在wsdl文档中,这些消息被定义message元素. 这些消息由称之为part元素的部分组成. 一个服务的操作,通过指定逻辑消息 ...

  7. socket-详细分析No buffer space available(转)

    新年上班第一天,突然遇到一个socket连接No buffer space available的问题,导致接口大面积调用(webservice,httpclient)失败的问题,重启服务器后又恢复了正 ...

  8. JAVA —— 数组

    import java.util.Arrays; public class Array { public static void main(String[] args){    Array test= ...

  9. 用Unicode迎接未来

         项目中使用了emoji,然后,问题产生了,后端MySQL数据库无法存储emoji字符,悲了个剧.      emoji是Unicode字符集的子集,Unicode的使用应该非常普遍了,怎么会 ...

  10. Android开发人员官方站点文档 - 国内踏得网镜像

    Android Developer 安卓开发人员官方站点无法正常訪问.即使FQ因为网络原因依旧訪问缓慢. 故整理相关字体.脚本.样式.页面资源,在踏得网server上建立了本地镜像.初始镜像时间201 ...