[抄题]:

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的结果]:

  1. 没看出来 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色阶中和器的更多相关文章

  1. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  2. 【Leetcode_easy】661. Image Smoother

    problem 661. Image Smoother 题意:其实类似于图像处理的均值滤波. solution: 妙处在于使用了一个dirs变量来计算邻域数值,看起来更简洁! class Soluti ...

  3. LeetCode 661. Image Smoother (图像平滑器)

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  4. LeetCode - 661. Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  5. [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 ...

  6. 661. Image Smoother@python

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  7. 【LeetCode】661. Image Smoother 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...

  8. 661. Image Smoother

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  9. 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

随机推荐

  1. PS基础教程[4]如何载入笔刷

    笔刷是我们制作图片时的一个很好的工具,能够快速方便的帮助我们制作出很多现有的效果,所以我们都会制作很多的笔刷保存起来载入到PS中方便我们使用.本次系类经验的第四篇就来介绍一下笔刷的导入. 方法 1.笔 ...

  2. hadoop1.x和2.x的一些主要区别

    当我们安装完毕hadoop2的时候,我们看到为啥没有jobtracker,这是因为hadoop2中已经没有jobtracer了,而是产生了yarn,yarn是什么那,可以看yarn详解,我们为什么已经 ...

  3. UIView+PYJExtension

    UIView+PYJExtension.h: // // UIView+PYJExtension.h // 扩展 // // Created by 彭运京 on 16/6/21. // Copyrig ...

  4. matlab 与 modelsim 联调 cic抽取滤波器

    注:本设计的参数为:D=2,R=5,N=3:时钟频率为50mhz,输入信号为有符号8位,根据公式bmax=bin+N*log(2,R*D):可以得到bmax=18: 1,cic抽取滤波器原理 网上资料 ...

  5. C++输入流和输出流、缓冲区

    一.C++输入流和输出流 输入和输出的概念是相对程序而言的. 键盘输入数据到程序叫标准输入,程序数据输出到显示器叫标准输出,标准输入和标准输出统称为标准I/O,文件的输入和输出叫文件I/O. cout ...

  6. yield关键字用法与解析(C# 参考)

    yield 关键字向编译器指示它所在的方法是迭代器块. 编译器生成一个类来实现迭代器块中表示的行为. 在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值. 这是一个返 ...

  7. maven搭建

    http://blog.csdn.net/zhshulin/article/details/30779873 http://blog.csdn.net/zhshulin/article/details ...

  8. (转)TextView 设置背景和文本颜色的问题

    在做一个项目,突然遇到如下问题 比如:在color.xml中定义了几个颜色 <color name="white">#FFFFFF</color> < ...

  9. Indy发送邮件被kbas退掉

    用indy开发了发送邮件程序,通过126,sina等发送邮件可以发送出去,而通过tom,163则被退回,显示被 kbas系统退回.后来通过观察Foxmail的通讯过程,区别在foxmail发送EHLO ...

  10. 2016女生赛 HDU 5710 Digit-Sum(数学,思维题)

    Digit-Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...