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. 洛谷 P1852 [国家集训队] 跳跳棋

    题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...

  2. 【AC自动机】【动态规划】poj3691 DNA repair

    http://blog.csdn.net/kk303/article/details/6929641 http://blog.csdn.net/human_ck/article/details/657 ...

  3. 1.8(学习笔记)监听器(Listener)

    一.监听器简介 监听器是web容器对某一些对象的监听,当某些对象发生创建.修改.删除的动作时, web容器会自动调用对应的监听器中的方法. 例如A是监听器,B是被监听对象,A的监听任务是B回家时来开门 ...

  4. Python自带的hmac模块

    Python自带的hmac模块实现了标准的Hmac算法 我们首先需要准备待计算的原始消息message,随机key,哈希算法,这里采用MD5,使用hmac的代码如下: import hmac mess ...

  5. redis系列文章

    http://blog.csdn.net/liubenlong007/article/details/53690103

  6. 扩展gridview轻松实现冻结行和列

    在实际的项目中,由于项目的需要,数据量比较大,同时显示栏位也比较多,要做gridview里显示完整,并做到用户体验比较好,这就需要冻结表头和关键列.由于用到的地方比较多,我们可以护展一个gridvie ...

  7. SQLite 使用技巧

    http://blog.csdn.net/beifengdelei/article/details/7166056 SQLite自增ID自段使用方法为 INTEGER PRIMARY KEY AUTO ...

  8. easyui datagrid加载成功之后选定并获取首行数据

    //加载成功之后,选定并获取首行数据 onLoadSuccess:function(data){ alert("grid加载成功"); var rows=$('test').dat ...

  9. phpmyadmin后台4种拿shell方法 && php爆路径大法

    php后台拿shell要知道php的路径,文章下面将讲诉爆php路径的方法!!! 方法一: CREATE TABLE `mysql`.`xss` (`xss1` TEXT NOT NULL ); IN ...

  10. 算法导论-散列表(Hash Table)-大量数据快速查找算法

    目录 引言 直接寻址 散列寻址 散列函数 除法散列 乘法散列 全域散列 完全散列 碰撞处理方法 链表法 开放寻址法 线性探查 二次探查 双重散列 随机散列 再散列问题 完整源码(C++) 参考资料 内 ...