73. 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?
代码:
题目很简单,就是矩阵中如果存在值为0的元素,就把该元素对应的行和列上面的元素全都改成0。
思考了半天,还是遍历了两遍,复杂度至少在O(mn):
public void setZeroes(int[][] matrix) {
int i,j;
ArrayList<Integer> row = new ArrayList<Integer>();
ArrayList<Integer> col = new ArrayList<Integer>();
//第一个循环,遍历一遍,记录哪些行和哪些列需要改成0
for (i=0;i < matrix.length;i++)
{
for (j=0;j<matrix[i].length;j++)
{
if(matrix[i][j] == 0)
{
row.add(i);
col.add(j);
}
}
}
//第二个循环,又遍历一遍,根据需要变为0的行和列修改对应元素
for (i=0;i < matrix.length;i++)
{
for (j=0;j<matrix[i].length;j++)
{
if(row.contains(i)||col.contains(j))
{
matrix[i][j] =0;
// System.out.print("修改第"+i+"行,第"+j+"列");
}
}
}
}
题目中提到算法复杂度可以小于 O(m + n),还在研究中...
73. Set Matrix Zeroes的更多相关文章
- 【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#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
- [LeetCode] 73. 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. Follow ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- LeetCode OJ 73. 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】73. 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. Fo ...
- 73. 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. 重点是空间复 ...
- 【一天一道LeetCode】#73. Set Matrix Zeroes
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0
[抄题]: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. ...
随机推荐
- 示波器trigger的使用方法
背景: 下位机有俩个IO口设置为外部中断——边沿触发.低电平有效.因此我需要抓取下降沿波形,但低电平时间很短,手动暂停抓取不仅不科学还费力,那么该如何准确的抓取到呢?最好的办法是使用示波器的trige ...
- Cocoa是什么?
一.什么是Cocoa ①Cocoa的来源 早些年,苹果公司启动了Copland计划,致力于开发出自己的操作系统,可惜后来Copland计划逐渐的失控了,苹果公司最终决定放弃开发,转向从别的公司购买下 ...
- 利用LruCache为GridView异步加载大量网络图片完整示例
MainActivity如下: package cc.testlrucache; import android.os.Bundle; import android.widget.GridView; i ...
- iOS开发——源代码管理——SVN
一.源代码管理(svn)简介 01. 源代码管理工具概述 ======================================================================= ...
- 语义化HTML
一.怎样语义化html结构 语义化是指用合理HTML标签以及其特有的属性去格式化文档内容. 正确语义化----结构(html)才是重点,样式(css)是用来修饰结构的.所以要先确定html,确定标签, ...
- 浅谈VBA
VBA,全称Visual Basic for Applications,其中的一些专业性的解释可以自行搜索,这里就不一一介绍.半年以前,我是不知道VBA的,当我听到VBA的时候,我却迷糊了.VBA是什 ...
- 2.实现Express中间件
Express提供的大部分功能都是通过中间件函数完成,这些中间件函数在Node.js收到 请求的时点 和 发送响应的时点 执行 connect模块提供了中间件框剪 方便在全局或路径级别或为单个路由插入 ...
- 再谈Weiphp公众平台开发——1、增加插件
去年开始接触基于Weiphp的公众平台开发,一直没时间好好整理一下. 下面开始讲解第一个自定义weiphp插件:MyHello的开发流程. 1.插件创建.在weiphp管理后台依次点击“插件管理-&g ...
- NDK学习4: Eclipse HelloWorld
NDK学习4: Eclipse HelloWorld 1.配置Eclipse NDK环境 Window->preferences->android->ndk 2.新建Andro ...
- SqlServer中的Null值空值问题
sql使用的是三值谓词逻辑,所以逻辑表达式返回的结果可以为True.False或者未知,在三值逻辑中返回True与不返回False并不完全一样, SQL对查询过滤条件的处理:接受TURE 拒绝FAL ...