Skip to content

Commit 213bd78

Browse files
committed
feat: add solutions to lc problem: No.0661
No.0661.Image Smoother
1 parent f970ee9 commit 213bd78

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

solution/0600-0699/0661.Image Smoother/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,85 @@
7575

7676
```
7777

78+
### **TypeScript**
79+
80+
```ts
81+
function imageSmoother(img: number[][]): number[][] {
82+
const m = img.length;
83+
const n = img[0].length;
84+
const locations = [
85+
[-1, -1],
86+
[-1, 0],
87+
[-1, 1],
88+
[0, -1],
89+
[0, 0],
90+
[0, 1],
91+
[1, -1],
92+
[1, 0],
93+
[1, 1],
94+
];
95+
96+
const res = [];
97+
for (let i = 0; i < m; i++) {
98+
res.push([]);
99+
for (let j = 0; j < n; j++) {
100+
let sum = 0;
101+
let count = 0;
102+
for (const [y, x] of locations) {
103+
if ((img[i + y] || [])[j + x] != null) {
104+
sum += img[i + y][j + x];
105+
count++;
106+
}
107+
}
108+
res[i].push(Math.floor(sum / count));
109+
}
110+
}
111+
return res;
112+
}
113+
```
114+
115+
### **Rust**
116+
117+
```rust
118+
impl Solution {
119+
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
120+
let m = img.len();
121+
let n = img[0].len();
122+
let locations = [
123+
[-1, -1],
124+
[-1, 0],
125+
[-1, 1],
126+
[0, -1],
127+
[0, 0],
128+
[0, 1],
129+
[1, -1],
130+
[1, 0],
131+
[1, 1],
132+
];
133+
134+
let mut res = vec![];
135+
for i in 0..m {
136+
res.push(vec![]);
137+
for j in 0..n {
138+
let mut sum = 0;
139+
let mut count = 0;
140+
for [y, x] in locations.iter() {
141+
let i = i as i32 + y;
142+
let j = j as i32 + x;
143+
if i < 0 || i == m as i32 || j < 0 || j == n as i32 {
144+
continue;
145+
}
146+
count += 1;
147+
sum += img[i as usize][j as usize];
148+
}
149+
res[i].push(sum / count);
150+
}
151+
}
152+
res
153+
}
154+
}
155+
```
156+
78157
### **...**
79158

80159
```

solution/0600-0699/0661.Image Smoother/README_EN.md

+79
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,85 @@ For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.8
5757

5858
```
5959

60+
### **TypeScript**
61+
62+
```ts
63+
function imageSmoother(img: number[][]): number[][] {
64+
const m = img.length;
65+
const n = img[0].length;
66+
const locations = [
67+
[-1, -1],
68+
[-1, 0],
69+
[-1, 1],
70+
[0, -1],
71+
[0, 0],
72+
[0, 1],
73+
[1, -1],
74+
[1, 0],
75+
[1, 1],
76+
];
77+
78+
const res = [];
79+
for (let i = 0; i < m; i++) {
80+
res.push([]);
81+
for (let j = 0; j < n; j++) {
82+
let sum = 0;
83+
let count = 0;
84+
for (const [y, x] of locations) {
85+
if ((img[i + y] || [])[j + x] != null) {
86+
sum += img[i + y][j + x];
87+
count++;
88+
}
89+
}
90+
res[i].push(Math.floor(sum / count));
91+
}
92+
}
93+
return res;
94+
}
95+
```
96+
97+
### **Rust**
98+
99+
```rust
100+
impl Solution {
101+
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
102+
let m = img.len();
103+
let n = img[0].len();
104+
let locations = [
105+
[-1, -1],
106+
[-1, 0],
107+
[-1, 1],
108+
[0, -1],
109+
[0, 0],
110+
[0, 1],
111+
[1, -1],
112+
[1, 0],
113+
[1, 1],
114+
];
115+
116+
let mut res = vec![];
117+
for i in 0..m {
118+
res.push(vec![]);
119+
for j in 0..n {
120+
let mut sum = 0;
121+
let mut count = 0;
122+
for [y, x] in locations.iter() {
123+
let i = i as i32 + y;
124+
let j = j as i32 + x;
125+
if i < 0 || i == m as i32 || j < 0 || j == n as i32 {
126+
continue;
127+
}
128+
count += 1;
129+
sum += img[i as usize][j as usize];
130+
}
131+
res[i].push(sum / count);
132+
}
133+
}
134+
res
135+
}
136+
}
137+
```
138+
60139
### **...**
61140

62141
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
impl Solution {
2+
pub fn image_smoother(img: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
3+
let m = img.len();
4+
let n = img[0].len();
5+
let locations = [
6+
[-1, -1],
7+
[-1, 0],
8+
[-1, 1],
9+
[0, -1],
10+
[0, 0],
11+
[0, 1],
12+
[1, -1],
13+
[1, 0],
14+
[1, 1],
15+
];
16+
17+
let mut res = vec![];
18+
for i in 0..m {
19+
res.push(vec![]);
20+
for j in 0..n {
21+
let mut sum = 0;
22+
let mut count = 0;
23+
for [y, x] in locations.iter() {
24+
let i = i as i32 + y;
25+
let j = j as i32 + x;
26+
if i < 0 || i == m as i32 || j < 0 || j == n as i32 {
27+
continue;
28+
}
29+
count += 1;
30+
sum += img[i as usize][j as usize];
31+
}
32+
res[i].push(sum / count);
33+
}
34+
}
35+
res
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function imageSmoother(img: number[][]): number[][] {
2+
const m = img.length;
3+
const n = img[0].length;
4+
const locations = [
5+
[-1, -1],
6+
[-1, 0],
7+
[-1, 1],
8+
[0, -1],
9+
[0, 0],
10+
[0, 1],
11+
[1, -1],
12+
[1, 0],
13+
[1, 1],
14+
];
15+
16+
const res = [];
17+
for (let i = 0; i < m; i++) {
18+
res.push([]);
19+
for (let j = 0; j < n; j++) {
20+
let sum = 0;
21+
let count = 0;
22+
for (const [y, x] of locations) {
23+
if ((img[i + y] || [])[j + x] != null) {
24+
sum += img[i + y][j + x];
25+
count++;
26+
}
27+
}
28+
res[i].push(Math.floor(sum / count));
29+
}
30+
}
31+
return res;
32+
}

0 commit comments

Comments
 (0)