Java for LeetCode 073 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.
解题思路:
用两个boolean数组row col表示行列是否有零即可,JAVA实现如下:
public void setZeroes(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0)
return;
boolean[] row = new boolean[matrix.length], col = new boolean[matrix[0].length];
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[0].length; j++)
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
for (int i = 0; i < matrix.length; i++)
for (int j = 0; j < matrix[i].length; j++)
if (row[i] || col[j])
matrix[i][j] = 0;
}
Java for LeetCode 073 Set Matrix Zeroes的更多相关文章
- Leetcode 073 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 ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [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. Exampl ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- Java for LeetCode 172 Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- Java for LeetCode 054 Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【LeetCode】Set Matrix Zeroes 解题报告
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...
- Leetcode#73 Set Matrix Zeroes
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...
随机推荐
- Freemarker 之 Java静态化 实例一
Freemarker是一种强大的web端模板技术,在当前Web开发中,SEO和客户端浏览速度尤为重要,其中将网页静态化是一个很好的解决方案.下面介绍Java中web开发结合Freemarker来实现静 ...
- 【POJ 1094】拓扑排序
题意 给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B).如果前i个关系可以确定n个字母的一个顺序就输出: Sorted sequence determined after i ...
- 【ZOJ 3502】Contest
题 题意 n个问题,解决的顺序影响正确的概率,无论之前解决的问题是否答对,当前问题 j 答对概率为max{a[i][j]} (i为解决过的问题).求答对题目的最大期望和对应的答题顺序.T组测试,T ( ...
- mysql库大小
1.进入information_schema 数据库(存放了其他的数据库的信息) use information_schema; 2.查询所有数据的大小: select concat(round(su ...
- Spring的辅助类
http://www.cnblogs.com/maoan/p/3446224.html spring获取ApplicationContext对象的方法——ApplicationContextAware
- 迪杰斯特拉(Java)
public class Dijsktra { public static void main(String[] args) { Dijsktra d=new Dijsktra(); int[][] ...
- py替换掉换行符
for line in file.readlines(): line=line.strip('\n')
- python二维数组
和c c++不一样 过程如下: #-*- coding:utf-8 -*- t = [[ 0 for i in range(5)]for j in range(5)] for i in range(5 ...
- 增强型for循环,用于遍历数组元素
/** * */ package com.cn.u4; /** * @author Administrator *增强型for */ public class ZhengQiangFor { publ ...
- nginx&apache比较
1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...