661. Image Smoother色阶中和器
[抄题]:
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself. If a cell has less than 8 surrounding cells, then use as many as you can.
Example 1:
Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
二维数组皆空、仅有行为空的返回情况不同
[思维问题]:
不知道怎么枚举所有情况
[一句话思路]:
冒号表达式+ 枚举数组可以列举所有情况
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
- 没看出来 if(valid函数)必须加括号
[总结]:
冒号表达式+ 枚举数组可以列举所有情况 类似于岛屿问题
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
冒号+列举数组
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int sum = 0, count = 0;
for (int incR : new int[]{-1, 0, 1}) {
for (int incC : new int[]{-1, 0, 1}) {
if (valid(row + incR, col + incC, rows, cols)) {
sum += M[row + incR][col + incC];
count++;
}
}
}
res[row][col] = sum / count;
}
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
行增加量命名为incR
class Solution {
public int[][] imageSmoother(int[][] M) {
//cc
if (M == null) return null;
int rows = M.length;
if (rows == 0) return new int[0][];
int cols = M[0].length; //ini
int[][] res = new int[rows][cols]; //for loop, sum / count
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
int sum = 0, count = 0;
for (int incR : new int[]{-1, 0, 1}) {
for (int incC : new int[]{-1, 0, 1}) {
if (valid(row + incR, col + incC, rows, cols)) {
sum += M[row + incR][col + incC];
count++;
}
}
}
res[row][col] = sum / count;
}
} //return res
return res;
} public boolean valid (int x, int y, int rows, int cols) {
if (x >= 0 && x < rows && y >= 0 && y <cols) return true;
return false;
}
}
661. Image Smoother色阶中和器的更多相关文章
- 661. Image Smoother【easy】
661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...
- 【Leetcode_easy】661. Image Smoother
problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...
- LeetCode 661. Image Smoother (图像平滑器)
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- LeetCode - 661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- [LeetCode&Python] Problem 661. Image Smoother
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 661. Image Smoother@python
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- 661. Image Smoother
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- 解决"hibernate.hbm2ddl.auto" update值 无效
<property name="schemaUpdate"> <value>true</value> </property> 若果是 ...
- Django实现微信公众号简单自动回复
在上篇博客阿里云部署django实现公网访问已经实现了了django在阿里云上的部署,接下来记录django实现微信公众号简单回复的开发过程,以方便日后查看 内容概要: (1)微信公众号声请 (2)微 ...
- Python使用教程
1.下载python windows:http://www.python.org/download/ 2.环境变量: 3.pycharm下载:http://www.python.org/downloa ...
- ZOJ-Big string(服气思维)
个人心得:我在分治上看到的,但是感觉跟分治没关系,一眼想到斐波那契数可以找到此时n的字符串,但是无法精确到字母,题解的思路 真是令人佩服,以BA为基准,然后只要此时的长度大于7那么必然可以减去最大的斐 ...
- 剑指offer-第六章面试中的各项能力(不用加减乘除做加法)
//不用加减乘除四则运算,来做加法 //题目:两个数做加法. //思路:用二进制的位运算的思路.第一步:首先两数相加考虑进位.可以用异或. //第二步:两个数相加只考虑进位,并将最后的结果左移.第三步 ...
- LeetCode Minimum Time Difference
原题链接在这里:https://leetcode.com/problems/minimum-time-difference/description/ 题目: Given a list of 24-ho ...
- 转载pll工作模式解析
PLL共有四种工作模式,只有理解了这四种工作模式的特点,才能在设计中选用恰当的模式,完成自己设计的预期功能.这四种工作模式分别是普通模式(Normal Mode).零延迟缓冲模式(Zero Delay ...
- 【spring源码学习】spring的AOP面向切面编程的实现解析
一:Advice(通知)(1)定义在连接点做什么,为切面增强提供织入接口.在spring aop中主要描述围绕方法调用而注入的切面行为.(2)spring定义了几个时刻织入增强行为的接口 => ...
- js删除局部变量的实现方法
lert('value:'+str+'\ttype:'+typeof(str)) //声明变量前,引用 var str="dd"; alert('value:'+str+'\tty ...
- 蓝桥杯 基础练习 BASIC-30 阶乘计算
基础练习 阶乘计算 时间限制:1.0s 内存限制:512.0MB 问题描述 输入一个正整数n,输出n!的值. 其中n!=1*2*3*…*n. 算法描述 n!可能很大,而计算机能表示的整数范围有 ...