661. Image Smoother@python
2024-09-02 08:50:03
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)
最新文章
- 使用SecureCRT连接虚拟机(ubuntu)配置记录
- Android实现圆形图片
- 基于HT for Web 3D技术快速搭建设备面板
- ext 自带搜索功能
- WCF初探-5:WCF消息交换模式之双工通讯(Duplex)
- shell script练习
- JavaScript获取两个时间的时间差
- Oracle中job的使用详解
- ASP.NET数据绑定控件
- Spring MVC的文件上传
- javascript预览图片——IT轮子系列(九)
- C++ 左值与右值 右值引用 引用折叠 =>; 完美转发
- Jenkins可持续集成项目搭建——配置邮件
- docker安装elasticsearch及head插件
- ModelViewSet 路由 / django logging配置 / django-debug-toolbar使用
- 剑指offer--2.替换空格
- 2.请介绍一下List和ArrayList的区别,ArrayList和HashSet区别
- C# enum、int、string三种类型互相转换
- 《FPGA全程进阶---实战演练》第三十二章 Signal Tap II 应用实例
- [转]MAC系统下Sublime Text3 配置Python3详细教程(亲测有效)
热门文章
- Metabolic Signatures of Cystic Fibrosis Identified in Dried Blood Spots For Newborn Screening Without Carrier Identification (文献分享一组-孔楠楠)
- iOS7 UITableView Row Height Estimation
- Python -3-列表和元组
- python模块之hmac
- urllib库的基本使用
- BZOJ1257(数论知识)
- python库使用整理
- httpd.exe占用100%CPU
- oracle 直接连接到数据库 CMD窗口
- iOS Category实现原理 (补充)