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 ...
随机推荐
- C++动态库的几点认识
1.动态库也有lib文件,称为导入库,一般大小只有几k: 2.动态库有静态调用和动态调用两种方式: 静态调用:使用.h和.lib文件 动态调用: 先LoadLibrary,再GetProcAddres ...
- JSOI2018 简要题解
潜入行动 复杂度分析题. 定义状态fi,j,0/1,0/1f_{i,j,0/1,0/1}fi,j,0/1,0/1表示以iii为根子树放jjj个机器iii这个放不放,iii这个是否已放来进行dpdpd ...
- 第50章:Java操作MongoDB-MongoDB和Spring
① Spring通过Spring Data MongoDB模块来集成和支持MongoDB ②Maven加入lib包 <dependency> <groupId>org.spri ...
- spring BeanWrapperImpl方便的嵌套属性(list)操作
beans 包主要提供了接口和类用于处理java beans. 其中最主要的接口是BeanWrapper: Spring 的中心接口,用于访问javabeans 的低层操作.默认实现为 ...
- Forward团队-爬虫豆瓣top250项目-最终程序
托管平台地址:https://github.com/xyhcq/top250 小组名称:Forward团队 小组成员合照: 程序运行方法: 在python中打开程序并运行:或者直接执行程序即可运行 程 ...
- 与http协作的web服务器--代理、网关、隧道
一台服务器可以搭建多个web站点 代理: 接受客户端发送的请求,转发给其他服务器,然后接受服务器的返回结果(响应)再返回给客户端.每次经过代理服务器,就会追加写入via首部信息. 按两种基准分类.一种 ...
- 理解jsonp劫持漏洞
JSONP劫持 存在漏洞的链接格式类似于以下这种: http://www.xxx.com/xxx.do?callback=info 参数名也常见有cb jsoncb call jsoncall cba ...
- nginx三种安装方法(转载)
Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.它最常的用途是提供反向代理服务. 1.安装包编译安装 2.yum源安装 3.使用 ...
- Lombok轮子
前提 自从进公司实习后,项目代码中能用 Lombok 的都用了,毕竟这么好的轮子要充分利用好.也可以减少一些 get/set/toString 方法的编写,虽说 IDEA 的插件可以自动生成 get/ ...
- BigDecimal比较2个值是否相等,不能用equals,而要用compareTo
BigDecimal比较相等,不能用equals,要用compareTo