forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
37 lines (36 loc) · 997 Bytes
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
impl Solution {
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
let m = img.len();
let n = img[0].len();
let locations = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 0],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
let mut res = vec![];
for i in 0..m {
res.push(vec![]);
for j in 0..n {
let mut sum = 0;
let mut count = 0;
for [y, x] in locations.iter() {
let i = i as i32 + y;
let j = j as i32 + x;
if i < 0 || i == m as i32 || j < 0 || j == n as i32 {
continue;
}
count += 1;
sum += img[i as usize][j as usize];
}
res[i].push(sum / count);
}
}
res
}
}