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 ...
随机推荐
- CodeForces - 981E Addition on Segments
考虑每个点i在什么情况下会成为最大值. 当选的区间子集是 包含i的区间的一个子集的时候,i肯定会是最大值. 所以我们就可以用这种方法得到所有点的可能的最大值是多少... 也就是说,最后的局面可以仅由一 ...
- [USACO2015DEC]Max Flow
题目大意: 给你一棵n个点的树,有m次操作,每次将给定的路径上所有点的点权+1. 问最后最大的点权是多少. 思路: #include<cstdio> #include<cctype& ...
- Java高级架构师(一)第10节:Spring+Mybatis实现DAO
maven配置memcached.jar 由于目前java memcached client没有官方的maven repository可供使用,因此使用时需要手动将其安装到本地repository. ...
- 收藏起来,史上最全的 MySQL 高性能优化实战总结!
转自:https://mp.weixin.qq.com/s/sRsJzFO9dPtKhovJNWN3Dg 一.前言 MySQL 对于很多 Linux 从业者而言,是一个非常棘手的问题,多数情况都是因为 ...
- iOS数据库的基本使用
今天总结下数据库的基本使用方法: iOS使用的数据库一般就是sqlite3,在使用该数据库前一定要先导入数据库框架,否则会出错,接下来引入头文件#import<sqlite3.h> 在工程 ...
- <摘录>字节对齐(强制对齐以及自然对齐)
struct {}node; 32为的x86,window下VC下sizeof(node)的值为1,而linux的gcc下值为0: 一.WINDOWS下(VC--其实GCC和其原理基本一样,象这种问题 ...
- android_我的第一个Android程序
今天开始学Android开发,搞了一下午就完成了两个小功能,大部分时间都在调试.熟悉环境, Android开发环境对比VS无论是安装.使用.更新都不够方便,不过慢慢适应就好 完成功能如下: 功能一 ...
- webpack-dev-server最简单的应用
1.安装 npm install webpack-dev-server --save-dev 2.再exports后加多一个对象即可 devServer: { contentBase: ". ...
- opencv实现camera模组的暗电流和lenshading补偿 .
目录(?)[-] 简介 基本原理 产生原因 校正补偿原理 具体实现 框架搭建 功能实现 暗电流 lenshading补偿 效果演示 图片处理 效果演示 简介 在接触过的qcom和mtk平台中,came ...
- solr File Upload "Unsupported ContentType: application/vnd.ms-excel Not in: [application/xml, application/csv, application/json, text/json, text/csv, text/xml, application/javabin]",
今天在用solr管理界面导入文件时报错:"Unsupported ContentType: application/vnd.ms-excel Not in: [application/xm ...