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:

  1. The value in the given matrix is in the range of [0, 255].
  2. 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】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  6. 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 ...

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

  8. 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 ...

  9. 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. [CTSC2017]密钥

    传送门:http://uoj.ac/problem/297 “无论哪场比赛,都要相信题目是水的” 这不仅是HNOI2018D2T3的教训,也是这题的教训,思维定势真的很可怕. 普及组水题,真是愧对CT ...

  2. small test on 5.30 morning T3

    经典的等价类计数问题,我们设 f(x) 为环长为 x 的时候的花环种类,那么答案显然等于 1/n  *  Σf( gcd (i,n) * [gcd(i,n)!=1] * [i>=0&&a ...

  3. java 获取系统信息及CPU的使用率(转)

    java 获取系统信息及CPU的使用率 原文:http://kakaluyi.javaeye.com/blog/211492 最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接 ...

  4. 1.4(Mybatis学习笔记)关联映射

    一.一对一 mybatis处理一对一主要通过<resultMap>中的<association>元素来处理. <association>元素主要使用方方式有两种: ...

  5. Spark1.4安装问题

    1)按照<大数据Spark企业级实战>第2章中的方法构建Spark集群,最后发现master可以正常启动,但是worker却都没有启动,原因是不能直接使用在slave模版文件 slaves ...

  6. Activity组件(传递数据)

    (一) 1.效果图:点击按钮“调用第二个Activity”,转到第二页面,之后点击“返回数据”,将第二个页面的数据传到第一个页面         2. activity_main.xml <?x ...

  7. JNI之数据类型

    1. JNIEnv 作用 JNIEnv 概念 : 是一个线程相关的结构体, 该结构体代表了 Java 在本线程的运行环境 ; JNIEnv 与 JavaVM : 注意区分这两个概念; -- JavaV ...

  8. Vue 单页应用:记事本

    若文章中存在内容无法加载的情况,请移步作者其他博客. 简书 CSDN 最近在看 Vue 的时候,别人给我安利了一个国外的小案例,通过 Vue 和 Vuex 来实现一个记事本. 仔细剖析下,发现“麻雀虽 ...

  9. 降低web服务器压力

    一.越来越多的并发连接数 现在的Web系统面对的并发连接数在近几年呈现指数增长,高并发成为了一种常态,给Web系统带来不小的挑战.以最简单粗暴的方式解决,就是增加Web系统的机器和升级硬件配置.虽然现 ...

  10. Xamarin.Forms 调用 腾讯地图SDK

    Xamarin.Forms研究了好一段时间了,最近一直在学习中,想尝试一下调用其他的SDK,就如腾讯地图SDK(申请容易). 完成此次项目得感谢以下链接: http://www.cnblogs.com ...