leetcode 661. Image Smoother
来源:互联网 发布:windows redis自启动 编辑:程序博客网 时间:2024/06/08 04:29
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].
本题题意很简单,直接暴力遍历即可
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;class Solution {public: vector<vector<int>> imageSmoother(vector<vector<int>>& m) { int row = m.size(), col = m[0].size(); vector<vector<int>> res(row, vector<int>(col, 0)); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { res[i][j] = getAverage(i, j, m); } } return res; } int getAverage(int x, int y, vector<vector<int>>& m) { int row = m.size(), col = m[0].size(); int sum = 0, count = 0; for (int i = max(0, x - 1); i <= min(row - 1, x + 1); i++) { for (int j = max(0, y - 1); j <= min(col - 1, y + 1); j++) { count++; sum += m[i][j]; } } return floor((double)sum / count); }};
- 【LeetCode】661. Image Smoother
- LeetCode 661. Image Smoother
- leetcode 661. Image Smoother
- leetcode 661. Image Smoother
- LeetCode 661. Image Smoother
- LeetCode-661. Image Smoother
- 【LeetCode】661. Image Smoother
- leetcode 661. Image Smoother
- Leetcode 661 Image Smoother
- leetcode 661 Image Smoother
- leetCode-Image Smoother
- 661. Image Smoother
- 661. Image Smoother(Java)
- 661. Image Smoother
- 661. Image Smoother
- 661. Image Smoother
- leetcode[Image Smoother]//待整理多种解法
- LeetCode 661 : Image Smoother(python)
- 假设某年的1月1日是星期一,问:这一年中的第x天,是星期几?
- chrome 63 增加了 Workspaces 2.0 可以直接编辑本地文件了
- Java 数据类型
- Windows下Python模块的安装
- 命令模式浅析
- leetcode 661. Image Smoother
- Spring Boot忽略https证书:No subject alternative names present
- fetch请求
- git常用命令
- 正则表达式(一)
- 剑指offer——旋转数组的最小数字
- 深度学习: Nonlinear (非线性)
- 在ubuntu下安装jdk
- Beamer中数学符号字体