661. Image Smoother【easy】
661. Image Smoother【easy】
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].
错误解法:
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int row = M.size();
int col = M[].size();
vector<vector<int>> temp(row + , vector<int>(col + ));
for (int j = ; j < col + ; ++j) {
temp[][j] = ;
}
for (int i = ; i < row + ; ++i) {
temp[i][] = ;
}
for (int j = ; j < col + ; ++j) {
temp[row][j] = ;
}
for (int i = ; i < row + ; ++i) {
temp[i][col] = ;
}
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
temp[i][j] = M[i - ][j - ];
}
}
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
int sum = ;
for (int x = -; x <= ; ++x) {
for (int y = -; y <= ; ++y) {
sum += temp[i + x][j + y];
}
}
temp[i][j] = floor(sum / );
}
}
vector<vector<int>> result(row, vector<int>(col));
for (int i = ; i < row; ++i) {
for (int j = ; j < col; ++j) {
result[i][j] = temp[i + ][j + ];
}
}
return result;
}
};

一开始我还想取巧,把边界扩充,想着可以一致处理,但是发现没有审清题意,坑了啊!
解法一:
class Solution {
private:
bool valid(int i,int j,vector<vector<int>>& M)
{
if (i >= && i<M.size() && j>= && j<M[].size())
return true;
return false;
}
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
vector<vector<int>> res;
if (M.size()== || M[].size()==)
return res;
for (int i = ; i< M.size(); i++)
{
vector<int> cur;
for(int j = ; j< M[].size(); j++)
{
int total = ;
int count = ;
for (int x = -; x<;x++)
{
for (int y = -; y<; y++)
{
if(valid(i+x,j+y,M))
{
count++;
total +=M[i+x][j+y];
}
}
}
cur.push_back(total/count);
}
res.push_back(cur);
}
return res;
}
};
中规中矩的解法,完全按照题目意思搞
解法三:
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int m = M.size(), n = M[].size();
if (m == || n == ) return {{}};
vector<vector<int>> dirs = {{,},{,-},{,},{-,},{-,-},{,},{-,},{,-}};
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
int sum = M[i][j], cnt = ;
for (int k = ; k < dirs.size(); k++) {
int x = i + dirs[k][], y = j + dirs[k][];
if (x < || x > m - || y < || y > n - ) continue;
sum += (M[x][y] & 0xFF);
cnt++;
}
M[i][j] |= ((sum / cnt) << );
}
}
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
M[i][j] >>= ;
}
}
return M;
}
};
真正的大神解法!大神解释如下:Derived from StefanPochmann's idea in "game of life": the board has ints in [0, 255], hence only 8-bit is used, we can use the middle 8-bit to store the new state (average value), replace the old state with the new state by shifting all values 8 bits to the right.
661. Image Smoother【easy】的更多相关文章
- 170. Two Sum III - Data structure design【easy】
170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- 141. Linked List Cycle【easy】
141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
随机推荐
- 1.7(学习笔记)过滤器(Fliter)
一.过滤器(Fliter)简介 过滤器是位于客户端与服务器之间的滤网,在访问资源时会经过一系列的过滤器, 满足条件则放行,不满足条件的将其拦截. 过滤器可以看做是一个特殊的Servlet,设置了过滤器 ...
- Bean的装配方式
(一) 知识点:Spring容器支持多种形式的Bean的装配方式,比如基于XML的装配,基于注解的装配和自动装配(最常用的就是基于注解的装配) Spring提供了两种基于xml的装配方式:设值注入(S ...
- 关于JS中原型链中的prototype与_proto_的个人理解与详细总结
一直认为原型链太过复杂,尤其看过某图后被绕晕了一整子,今天清理硬盘空间(渣电脑),偶然又看到这图,勾起了点回忆,于是索性复习一下原型链相关的内容,表达能力欠缺逻辑混乱别见怪(为了防止新人__(此处指我 ...
- u-boot中添加mtdparts支持以及Linux的分区设置
简介 作者:彭东林 邮箱:pengdonglin137@163.com u-boot版本:u-boot-2015.04 Linux版本:Linux-3.14 硬件平台:tq2440, 内存:64M ...
- 关于接口 RandomAccess
今天看到java.util.Collections这个工具类中的 public static <T> void fill(List<? super T> list, T obj ...
- nagios监控redis
nagios是非常强大的监控工具,但是它本身没有监控redis的功能 但是网上有很多大神写了监控redis的插件,比较热门的使用perl写的check_redis.pl 但是由于我们监控mongodb ...
- 【AS3 Coder】任务九:游戏新手引导的制作原理(下)
在上一篇教程中,我们了解了一套我自创的新手引导管理框架的使用原理,那么在本篇教程中,我们将考虑新手引导制作中可能遇到的一些棘手问题及探讨其解决方案.Are you ready my baby? Let ...
- ./test.sh . ./test.sh source ./test.sh的区别
背景 今天写几个shell脚本,使用一个公共的config.sh,但是export出来的东西在另外的*.sh中不能直接用,这让我很惆怅,印象中就是可以export出来给别的shell用啊,只要不是下一 ...
- Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)
转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...
- 使用InstallUtil对Windows服务进行安装与卸载
关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...