leetcode — set-matrix-zeroes
import java.util.Arrays;
/**
* Source : https://oj.leetcode.com/problems/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 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?
*
*/
public class SetMatrixsZero {
/**
* 将原矩阵中为0元素所在的行列set为0
* 使用不同空间的算法
* O(mn):复制一个和原来矩阵一样的矩阵,遍历原矩阵,遇到为0的元素,设置复制矩阵的行列为0
* O(m+n):使用一个row[m]记录0至(m-1)行需要置为0的行,一个col[n]数组记录0至(n-1)列需要被置为0的列,最后遍历两个数组对相应行列置位
* O(1):因为如果某一个位置出现了0,那么该行和该列都要被置为0,所以该行的第一列最后会变为0,也就是会抹除原来的数字,
* 该列的第一行也类似,所以第一行和第一列可以代替上面这种方法中的两个数组,用来记录该行该列需要被置为0的情况,
* 但是第0行和第0列需要额外的两个变量来记录,所以占用空间是常数(利用了原矩阵中的第0行和第0列)
*
* 下面实现占用常数空间的方法
*
* @param matrix
* @return
*/
public int[][] findZero (int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return matrix;
}
int firstRow = 1;
int firstCol = 1;
// 判断第0行和第0列
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][0] == 0) {
firstCol = 0;
break;
}
}
for (int i = 0; i < matrix[0].length; i++) {
if (matrix[0][i] == 0) {
firstRow = 0;
break;
}
}
// 遍历第1-(m-1)行和1-(n-1)列
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
setZero(matrix, firstRow, firstCol);
return matrix;
}
/**
*
* @param matrix
* @param firstRow
* @param firstCol
*/
public void setZero (int[][] matrix, int firstRow, int firstCol) {
for (int i = 1; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (firstCol == 0) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
if (firstRow == 0) {
for (int i = 0; i < matrix[0].length; i++) {
matrix[0][i] = 0;
}
}
}
public static void printMatrix (int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
System.out.println(Arrays.toString(matrix[i]));
}
System.out.println();
}
public static void main(String[] args) {
SetMatrixsZero setMatrixsZero = new SetMatrixsZero();
int[][] arr = new int[][]{};
int[][] arr1 = new int[][]{
{1}
};
int[][] arr2 = new int[][]{
{0}
};
int[][] arr3 = new int[][]{
{1,0,1}
};
int[][] arr4 = new int[][]{
{1},
{0},
{1}
};
int[][] arr5 = new int[][]{
{1,2,3},
{0,3,4},
{1,0,4}
};
printMatrix(setMatrixsZero.findZero(arr));
printMatrix(setMatrixsZero.findZero(arr1));
printMatrix(setMatrixsZero.findZero(arr2));
printMatrix(setMatrixsZero.findZero(arr3));
printMatrix(setMatrixsZero.findZero(arr4));
printMatrix(setMatrixsZero.findZero(arr5));
}
}
leetcode — set-matrix-zeroes的更多相关文章
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...
- LeetCode OJ--Set Matrix Zeroes **
http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...
- 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. 原题链接:h ...
- LeetCode Set Matrix Zeroes(技巧+逻辑)
题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...
- [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解题报告—— 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
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 ...
随机推荐
- rn下的弹性布局
重点: 1]react native 下的弹性布局名字叫:flexDirection 2]flexDirection的默认值是column而不是row,而flex也只能指定一个数字值. 3]使用fle ...
- linux下PythonQt混编记录
看了很多博主经验,然后自己实验总结出来的,就不一一写出处了,非常感谢各位博主的分享经验. 一.安装Python3.6 安装依赖环境 # yum -y install zlib-devel bzip2- ...
- gulp使用详解
前面整理了 gulp使用入门,本节介绍相关的压缩 1.压缩js文件 步骤同基础步骤,相关可以看入门篇 npm install gulp-uglify --save-dev 引入js压缩库 gulpfi ...
- sshd_config 配置文件参数详解
sshd_config配置详解 名称sshd_config - OpenSSH SSH 服务器守护进程配置文件 大纲/etc/ssh/sshd_config 描述sshd(8) 默认从 /etc/ss ...
- ADO.NET学习笔记(1)
ADO.Net是.Net框架中为数据库的访问而封装的一个库.通过这个库我们可以简单便捷的访问数据库,并对数据库进行一些增删改查的操作,目前ADO.Net支持四种主流的数据库,分别是SQL.OLE DB ...
- JS入门经典第二章总结
document:在对网页编写脚本时,我们使用document对象代表网页.要引用一个属性,只需在document对象后加一个“.”号,然后再加上要引用的属性名. alert():该函数弹出一个消息框 ...
- Eigen3安装及注意
执行命令: sudo apt-get install libeigen3-dev 安装后执行以下命令 运行命令: sudo cp -r /usr/include/eigen3/Eigen /usr/i ...
- C++面试基础之回调
回调函数技术广泛运用在动态库开发(或者类库)中,是使软件模块化的重要手段.回调函数可以看作是一种通知和实现机制,用于控制反转,即模块A调用模块B时,模块B完成一定任务后反过头来调用模块A.在被调用方代 ...
- 无法从项目中获取SSIS包的列表
一直做的SSIS项目,突然在生成项目的时候没有反应,crtl + alt +o 提示:无法从项目中获取SSIS包的列表,发现是最近的包没有设计数据源, 解决思路:检查最近的包,挨个运行一遍,看看有没有 ...
- jquery mobile Touch事件
Touch事件在用户触摸屏幕(页面)时触发 1.jquery mobile tap tap事件在用户敲击某个元素时触发 $("p").on("tap",fucn ...