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. 【枚举】bzoj1800 [Ahoi2009]fly 飞行棋

    暴力枚举. #include<cstdio> #include<algorithm> using namespace std; ],sum[],half,ans; int qu ...

  2. [POI2001]Peaceful Commission

    题目大意: 有n个国家要派代表开会,每个国家有两个代表可供选择. 有m对代表有仇,不能同时开会. 若每个国家只能派一个代表开会,问是否存在一种方案,使得每个国家都能正常参会? 如果有,输出字典序最小的 ...

  3. VHDL硬件描述语言实现数字钟

    --VHDL上机的一个作业,程序太长实验报告册上写不下了.于是就在博客上留一份吧.LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOG ...

  4. Android UI 统一修改Button控件的样式,以及其它系统控件的默认样式

    先介绍下修改原理:首先打开位于android.widget包下面的Button.java文件,这里有一句关键的代码如下: public Button(Context context, Attribut ...

  5. RabbitMQ技术详解(转)

    RabbitMQ是什么 定义 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java.JMS.C.PHP.Action ...

  6. Java下List<Long>转List<String>或者List<Long>转List<Integer>

    说明:很遗憾,没有快速方法,只能遍历然后循环增加进去. 方法: for(String str : list) { int i = Integer.paseInt(str); intList.add(i ...

  7. Java读取文本文件

    try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw StringBuilder stringBuilder = new StringBuilder(); // 读入 ...

  8. iOS: iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  9. 解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】

    问题描述:当SSH远程连接到服务器上,然后运行一个服务 ./catalina.sh start,然后把终端开闭(切断SSH连接)之后,发现该服务中断,导致网页无法访问.   解决方法:使用nohup命 ...

  10. 小伙子又乱码了吧-Java字符编码原理总结

    前提 配合前面阅读的I/O和NIO的资料,现在总结一下关于字符集和乱码问题的原理和解决方案.参考资料: 码表ASCII Unicode GBK UTF-8 字符编码笔记ASCII,Unicode和UT ...