661. Image Smoother@python
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
Note:
- The value in the given matrix is in the range of [0, 255].
- The length and width of the given matrix are in the range of [1, 150].
原题地址: Image Smoother
难度: Easy
题意: 平滑图片, 每一个点值为其四周(包含自身)的平均值
思路:
一个二维数组, 按照题意,点可以分为三类
(1)四个角的点, 其周围有4个点(包含自身)
(2)二维数组最外层除了四个角的点,其周围有6个点(包含自身)
(3)其他的点(内层点),其周围有9个点(包含自身)
直接暴力解决,遍历数组
代码:
class Solution(object):
def imageSmoother(self, M):
"""
:type M: List[List[int]]
:rtype: List[List[int]]
"""
m = len(M)
n = len(M[0])
res = [[0] * n for i in range(m)]
if m <= 1 and n <= 1:
return M
if m == 1 and n > 1:
res[0][0] = (M[0][0] + M[0][1]) // 2
res[0][-1] = (M[0][-1] + M[0][-2]) // 2
for j in range(1, n-1):
res[0][j] = sum(M[0][j-1: j+2]) // 3
return res if n == 1 and m > 1:
res[0][0] = (M[0][0] + M[1][0]) // 2
res[-1][0] = (M[-1][0] + M[-2][0]) // 2
for i in range(1, m-1):
res[i][0] = (M[i-1][0] + M[i][0] + M[i+1][0]) // 3
return res for i in range(m):
for j in range(n):
if i == 0 and j == 0:
res[i][j] = (M[i][j] + M[i][j+1] + M[i+1][j] + M[i+1][j+1]) // 4
if i == 0 and j == n-1:
res[i][j] = (M[i][j] + M[i][j-1] + M[i+1][j] + M[i+1][j-1]) // 4
if i == m-1 and j == 0:
res[i][j] = (M[i][j] + M[i][j+1] + M[i-1][j] + M[i-1][j+1]) // 4
if i == m-1 and j == n-1:
res[i][j] = (M[i][j] + M[i][j-1] + M[i-1][j] + M[i-1][j-1]) // 4 if i == 0 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i+1][j-1: j+2])) // 6
if i == m-1 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i-1][j-1: j+2])) // 6 if j == 0 and 0 < i < m-1:
res[i][j] = (sum(M[i][j: j+2]) + sum(M[i-1][j: j+2]) + sum(M[i+1][j: j+2])) // 6 if j == n-1 and 0 < i < m-1:
res[i][j] = (sum(M[i][j-1: j+1]) + sum(M[i-1][j-1: j+1]) + sum(M[i+1][j-1: j+1])) // 6 if 0 < i < m -1 and 0 < j < n-1:
res[i][j] = (sum(M[i][j-1: j+2]) + sum(M[i-1][j-1: j+2]) + sum(M[i+1][j-1: j+2])) // 9 return res
时间复杂度: O(mn)
空间复杂度: O(1)
661. Image Smoother@python的更多相关文章
- 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&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 ...
- 【LeetCode】661. Image Smoother 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解决 日期 题目地址:https://l ...
- 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 ...
- 661. Image Smoother色阶中和器
[抄题]: Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoo ...
- 661. Image Smoother
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- Leetcode with Python -> Array
118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
随机推荐
- [Xcode 实际操作]九、实用进阶-(18)图像人脸识别:对图片中的人像进行面部检测
目录:[Swift]Xcode实际操作 本文将演示对图片中的人像,进行面部检测. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //导入 ...
- 密码破解工具John the Ripper使用说明
John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...
- [Noip模拟题]统计方案
题目并不难,想一下就会了,我真的智商持续下降,取模情况下做除法我都没想到逆元. 总之想到逆元就好写了,还是\(meet\ in\ the\ middle\)裸题,数组开不下用\(hash/map\)存 ...
- [Usaco2011 Dec]Grass Planting
Description Farmer John has N barren pastures connected by N-1 bidirectional roads, such that there ...
- Qt 进程和线程之一:运行一个进程和进程间通信
Qt提供了对进程和线程的支持.本节讲述了怎样在Qt应用程序中启动一个进程,以及几种常用的进程间通信方法.如果对进程和线程的概念不是很了解,可以看我的另一篇博客:[多进程和多线程的概念. 设计应用程序时 ...
- Guard Duty (hard) Codeforces - 958E3 || uva 1411
https://codeforces.com/contest/958/problem/E3 当没有三点共线时,任意一个这样的点集都是保证可以找到答案的,(考虑任意一种有相交的连线方案,一定可以将其中两 ...
- python之内置函数(lambda,sorted,filter,map),递归,二分法
一.lambda匿名函数 为了解决一些简单需求而设计的一句话函数,lambda表示的是匿名函数,不需要用def来声明,一句话就可以声明出一个函数. 语法: 函数名 = lambda 参数 : 返回值 ...
- [转]AngularJS:何时应该使用Directive、Controller、Service?
AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉.(译者注:老外真谦虚,我大天朝的码农对这些概念那是相当熟悉啊!)这些概念有: Directi ...
- Spring AOP初步总结(一)
学习AOP有段时间了,一直没空总结一下,导致有些知识点都遗忘了,之后会把以前学过的Spring核心相关的知识点总结一轮... 先大体介绍下Spring AOP的特点(均摘自"Spring i ...
- Sublime的用法
一.首先安装插件 1.安装Package Control,这是为了安装其他插件做基础,它可以方便我们下载其他插件 (1).按Ctrl+`调出console(注:避免热键冲突) (2).粘贴以下代码到命 ...