661. Image Smoother
Дан целочисленный матрица img размером m x n, представляющая градации серого изображения. Верните изображение после применения сглаживания к каждой его ячейке.
Пример
Input: img = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[0,0,0],[0,0,0],[0,0,0]]
Explanation:
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the points (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
C# решение
сопоставлено/оригиналpublic class Solution {
public int[][] ImageSmoother(int[][] img) {
int m = img.Length, n = img[0].Length;
int[][] result = new int[m][];
for (int i = 0; i < m; i++) {
result[i] = new int[n];
for (int j = 0; j < n; j++) {
int count = 0, total = 0;
for (int ni = Math.Max(0, i - 1); ni <= Math.Min(m - 1, i + 1); ni++) {
for (int nj = Math.Max(0, j - 1); nj <= Math.Min(n - 1, j + 1); nj++) {
total += img[ni][nj];
count++;
}
}
result[i][j] = total / count;
}
}
return result;
}
}
C++ решение
auto-draft, проверить перед отправкой#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int[][] ImageSmoother(int[][] img) {
int m = img.size(), n = img[0].size();
int[][] result = new int[m][];
for (int i = 0; i < m; i++) {
result[i] = new int[n];
for (int j = 0; j < n; j++) {
int count = 0, total = 0;
for (int ni = max(0, i - 1); ni <= min(m - 1, i + 1); ni++) {
for (int nj = max(0, j - 1); nj <= min(n - 1, j + 1); nj++) {
total += img[ni][nj];
count++;
}
}
result[i][j] = total / count;
}
}
return result;
}
}
Java решение
сопоставлено/оригиналpublic class Solution {
public int[][] imageSmoother(int[][] img) {
int m = img.length, n = img[0].length;
int[][] result = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int count = 0, total = 0;
for (int ni = Math.max(0, i - 1); ni <= Math.min(m - 1, i + 1); ni++) {
for (int nj = Math.max(0, j - 1); nj <= Math.min(n - 1, j + 1); nj++) {
total += img[ni][nj];
count++;
}
}
result[i][j] = total / count;
}
}
return result;
}
}
JavaScript решение
сопоставлено/оригиналvar imageSmoother = function(img) {
let m = img.length, n = img[0].length;
let result = Array.from({ length: m }, () => Array(n).fill(0));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let count = 0, total = 0;
for (let ni = Math.max(0, i - 1); ni <= Math.min(m - 1, i + 1); ni++) {
for (let nj = Math.max(0, j - 1); nj <= Math.min(n - 1, j + 1); nj++) {
total += img[ni][nj];
count++;
}
}
result[i][j] = Math.floor(total / count);
}
}
return result;
};
Go решение
сопоставлено/оригиналfunc imageSmoother(img [][]int) [][]int {
m, n := len(img), len(img[0])
result := make([][]int, m)
for i := range result {
result[i] = make([]int, n)
for j := 0; j < n; j++ {
count, total := 0, 0
for ni := max(0, i-1); ni <= min(m-1, i+1); ni++ {
for nj := max(0, j-1); nj <= min(n-1, j+1); nj++ {
total += img[ni][nj]
count++
}
}
result[i][j] = total / count
}
}
return result
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
Algorithm
Инициализация:
Создайте новую матрицу такого же размера, чтобы сохранить результат сглаживания.
Обработка каждой ячейки:
Для каждой ячейки исходной матрицы найдите всех её соседей (включая саму ячейку).
Вычислите среднее значение этих ячеек и сохраните его в соответствующей ячейке результирующей матрицы.
Возврат результата:
Верните результирующую матрицу после применения сглаживания ко всем ячейкам.
😎
Вакансии для этой задачи
Показаны активные вакансии с пересечением по тегам задачи.