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 ...
随机推荐
- WinDbg分析Dump常用方法和命令
记录下自己使用WinDbg分析Dump时常用的一些方法和命令 !analyze -v //找出出错的堆 .exrc //找到程序崩溃的位置 !heap //打印出错函数的局部位置 !for_each_ ...
- explicit_defaults_for_timestamp引发的狗血剧情
今天就碰到了一个较初级的问题,居然为找这个参数花了好半天时间,深以为不齿.需求是这样的,有个表的某个字段需要从datetime改成timestamp类型.原结构如下:create table tmp1 ...
- Linux 目录结构详解
Linux目录详解 Linux目录详解(RHEL5.4) 由于linux是开放源代码,各大公司和团体根据linux的核心代码做各自的操作,编程.这样就造成在根下的目录的不同.这样就造成个人不能使用他人 ...
- 移动端布局:视口viewport的理解
移动端开发中,有一些基本概念需要理解清楚,才能更好的组织编程逻辑.在刚接触时,移动端视口的缩放和rem单位的缩放搞混淆了,弄得自己很蒙圈.所以仔细总结下自己的理解. 移动端的适配,我理解为两点: 第一 ...
- Breathe me
Help, I have done it again 帮帮我,我又做错了. I have been here many times before 哪怕这已经不是一两次了. Hurt myself ag ...
- kubernets基础
1.定义和功能. 1.1定义:kubernets解释为舵手或者飞行员,以Borg为主衍生出. 1.2功能:自动装箱,自我修复,水平扩展,服务发现和负载均衡,自动发布和回滚. 密钥和配置管理,存储编排, ...
- junit 方法:assertEquals 和 assertTrue
assertEquals 和 assertTrue 区别相同之处:都能判断两个值是否相等 assertTrue 如果为true,则运行success,反之Failure assertEquals 如果 ...
- pyqt5 graphics view简单使用
Graphics View提供了一个平面,用于管理和交互大量自定义的2D图形图元,以及一个用于可视化图元的视图窗口小部件,支持缩放和旋转. 该框架包括一个事件传播架构,允许场景中图元的精确双精度交互功 ...
- 插入排序之Java实现
插入排序类似于大多数人安排扑克牌的方式. 1.从你手中的一张牌开始, 2.选择下一张卡并将其插入到正确的排序顺序中, 3.对所有的卡重复上一步. /** * * 代码理解:只需要记住两点: * 1.当 ...
- eclipse项目无故报错,markers信息为An error occurred while filtering resources
eclipse项目无故报错,markers信息为An error occurred while filtering resources 描述:eclipse项目和resource文件上有红色的叉,其m ...