Skip to content

Commit 4fe429b

Browse files
authored
feat: add solutions to lc problems: No.0531,0533 (doocs#2628)
* No.0531.Lonely Pixel I * No.0533.Lonely Pixel II
1 parent 18ed78e commit 4fe429b

File tree

14 files changed

+529
-507
lines changed

14 files changed

+529
-507
lines changed

solution/0500-0599/0531.Lonely Pixel I/README.md

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,30 @@
4242

4343
## 解法
4444

45-
### 方法一
45+
### 方法一:计数 + 枚举
46+
47+
根据题目描述,我们需要统计每一行和每一列的黑色像素数量,分别记录在数组 $\textit{rows}$ 和 $\textit{cols}$ 中。然后我们遍历每一个黑色像素,检查其所在的行和列是否只有一个黑色像素,如果是则将答案加一。
48+
49+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别是矩阵的行数和列数。
4650

4751
<!-- tabs:start -->
4852

4953
```python
5054
class Solution:
5155
def findLonelyPixel(self, picture: List[List[str]]) -> int:
52-
m, n = len(picture), len(picture[0])
53-
rows, cols = [0] * m, [0] * n
54-
for i in range(m):
55-
for j in range(n):
56-
if picture[i][j] == 'B':
56+
rows = [0] * len(picture)
57+
cols = [0] * len(picture[0])
58+
for i, row in enumerate(picture):
59+
for j, x in enumerate(row):
60+
if x == "B":
5761
rows[i] += 1
5862
cols[j] += 1
59-
res = 0
60-
for i in range(m):
61-
if rows[i] == 1:
62-
for j in range(n):
63-
if picture[i][j] == 'B' and cols[j] == 1:
64-
res += 1
65-
break
66-
return res
63+
ans = 0
64+
for i, row in enumerate(picture):
65+
for j, x in enumerate(row):
66+
if x == "B" and rows[i] == 1 and cols[j] == 1:
67+
ans += 1
68+
return ans
6769
```
6870

6971
```java
@@ -80,18 +82,15 @@ class Solution {
8082
}
8183
}
8284
}
83-
int res = 0;
85+
int ans = 0;
8486
for (int i = 0; i < m; ++i) {
85-
if (rows[i] == 1) {
86-
for (int j = 0; j < n; ++j) {
87-
if (picture[i][j] == 'B' && cols[j] == 1) {
88-
++res;
89-
break;
90-
}
87+
for (int j = 0; j < n; ++j) {
88+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
89+
++ans;
9190
}
9291
}
9392
}
94-
return res;
93+
return ans;
9594
}
9695
}
9796
```
@@ -111,47 +110,65 @@ public:
111110
}
112111
}
113112
}
114-
int res = 0;
113+
int ans = 0;
115114
for (int i = 0; i < m; ++i) {
116-
if (rows[i] == 1) {
117-
for (int j = 0; j < n; ++j) {
118-
if (picture[i][j] == 'B' && cols[j] == 1) {
119-
++res;
120-
break;
121-
}
115+
for (int j = 0; j < n; ++j) {
116+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
117+
++ans;
122118
}
123119
}
124120
}
125-
return res;
121+
return ans;
126122
}
127123
};
128124
```
129125
130126
```go
131-
func findLonelyPixel(picture [][]byte) int {
132-
m, n := len(picture), len(picture[0])
133-
rows := make([]int, m)
134-
cols := make([]int, n)
135-
for i := 0; i < m; i++ {
136-
for j := 0; j < n; j++ {
137-
if picture[i][j] == 'B' {
127+
func findLonelyPixel(picture [][]byte) (ans int) {
128+
rows := make([]int, len(picture))
129+
cols := make([]int, len(picture[0]))
130+
for i, row := range picture {
131+
for j, x := range row {
132+
if x == 'B' {
138133
rows[i]++
139134
cols[j]++
140135
}
141136
}
142137
}
143-
res := 0
144-
for i := 0; i < m; i++ {
145-
if rows[i] == 1 {
146-
for j := 0; j < n; j++ {
147-
if picture[i][j] == 'B' && cols[j] == 1 {
148-
res++
149-
break
150-
}
138+
for i, row := range picture {
139+
for j, x := range row {
140+
if x == 'B' && rows[i] == 1 && cols[j] == 1 {
141+
ans++
151142
}
152143
}
153144
}
154-
return res
145+
return
146+
}
147+
```
148+
149+
```ts
150+
function findLonelyPixel(picture: string[][]): number {
151+
const m = picture.length;
152+
const n = picture[0].length;
153+
const rows: number[] = Array(m).fill(0);
154+
const cols: number[] = Array(n).fill(0);
155+
for (let i = 0; i < m; ++i) {
156+
for (let j = 0; j < n; ++j) {
157+
if (picture[i][j] === 'B') {
158+
++rows[i];
159+
++cols[j];
160+
}
161+
}
162+
}
163+
let ans = 0;
164+
for (let i = 0; i < m; ++i) {
165+
for (let j = 0; j < n; ++j) {
166+
if (picture[i][j] === 'B' && rows[i] === 1 && cols[j] === 1) {
167+
++ans;
168+
}
169+
}
170+
}
171+
return ans;
155172
}
156173
```
157174

solution/0500-0599/0531.Lonely Pixel I/README_EN.md

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,30 @@
3838

3939
## Solutions
4040

41-
### Solution 1
41+
### Solution 1: Counting + Enumeration
42+
43+
According to the problem description, we need to count the number of black pixels in each row and column, which are recorded in the arrays `rows` and `cols` respectively. Then we traverse each black pixel, check whether there is only one black pixel in its row and column. If so, we increment the answer by one.
44+
45+
The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$, where $m$ and $n$ are the number of rows and columns in the matrix respectively.
4246

4347
<!-- tabs:start -->
4448

4549
```python
4650
class Solution:
4751
def findLonelyPixel(self, picture: List[List[str]]) -> int:
48-
m, n = len(picture), len(picture[0])
49-
rows, cols = [0] * m, [0] * n
50-
for i in range(m):
51-
for j in range(n):
52-
if picture[i][j] == 'B':
52+
rows = [0] * len(picture)
53+
cols = [0] * len(picture[0])
54+
for i, row in enumerate(picture):
55+
for j, x in enumerate(row):
56+
if x == "B":
5357
rows[i] += 1
5458
cols[j] += 1
55-
res = 0
56-
for i in range(m):
57-
if rows[i] == 1:
58-
for j in range(n):
59-
if picture[i][j] == 'B' and cols[j] == 1:
60-
res += 1
61-
break
62-
return res
59+
ans = 0
60+
for i, row in enumerate(picture):
61+
for j, x in enumerate(row):
62+
if x == "B" and rows[i] == 1 and cols[j] == 1:
63+
ans += 1
64+
return ans
6365
```
6466

6567
```java
@@ -76,18 +78,15 @@ class Solution {
7678
}
7779
}
7880
}
79-
int res = 0;
81+
int ans = 0;
8082
for (int i = 0; i < m; ++i) {
81-
if (rows[i] == 1) {
82-
for (int j = 0; j < n; ++j) {
83-
if (picture[i][j] == 'B' && cols[j] == 1) {
84-
++res;
85-
break;
86-
}
83+
for (int j = 0; j < n; ++j) {
84+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
85+
++ans;
8786
}
8887
}
8988
}
90-
return res;
89+
return ans;
9190
}
9291
}
9392
```
@@ -107,47 +106,65 @@ public:
107106
}
108107
}
109108
}
110-
int res = 0;
109+
int ans = 0;
111110
for (int i = 0; i < m; ++i) {
112-
if (rows[i] == 1) {
113-
for (int j = 0; j < n; ++j) {
114-
if (picture[i][j] == 'B' && cols[j] == 1) {
115-
++res;
116-
break;
117-
}
111+
for (int j = 0; j < n; ++j) {
112+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
113+
++ans;
118114
}
119115
}
120116
}
121-
return res;
117+
return ans;
122118
}
123119
};
124120
```
125121
126122
```go
127-
func findLonelyPixel(picture [][]byte) int {
128-
m, n := len(picture), len(picture[0])
129-
rows := make([]int, m)
130-
cols := make([]int, n)
131-
for i := 0; i < m; i++ {
132-
for j := 0; j < n; j++ {
133-
if picture[i][j] == 'B' {
123+
func findLonelyPixel(picture [][]byte) (ans int) {
124+
rows := make([]int, len(picture))
125+
cols := make([]int, len(picture[0]))
126+
for i, row := range picture {
127+
for j, x := range row {
128+
if x == 'B' {
134129
rows[i]++
135130
cols[j]++
136131
}
137132
}
138133
}
139-
res := 0
140-
for i := 0; i < m; i++ {
141-
if rows[i] == 1 {
142-
for j := 0; j < n; j++ {
143-
if picture[i][j] == 'B' && cols[j] == 1 {
144-
res++
145-
break
146-
}
134+
for i, row := range picture {
135+
for j, x := range row {
136+
if x == 'B' && rows[i] == 1 && cols[j] == 1 {
137+
ans++
147138
}
148139
}
149140
}
150-
return res
141+
return
142+
}
143+
```
144+
145+
```ts
146+
function findLonelyPixel(picture: string[][]): number {
147+
const m = picture.length;
148+
const n = picture[0].length;
149+
const rows: number[] = Array(m).fill(0);
150+
const cols: number[] = Array(n).fill(0);
151+
for (let i = 0; i < m; ++i) {
152+
for (let j = 0; j < n; ++j) {
153+
if (picture[i][j] === 'B') {
154+
++rows[i];
155+
++cols[j];
156+
}
157+
}
158+
}
159+
let ans = 0;
160+
for (let i = 0; i < m; ++i) {
161+
for (let j = 0; j < n; ++j) {
162+
if (picture[i][j] === 'B' && rows[i] === 1 && cols[j] === 1) {
163+
++ans;
164+
}
165+
}
166+
}
167+
return ans;
151168
}
152169
```
153170

solution/0500-0599/0531.Lonely Pixel I/Solution.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ class Solution {
1212
}
1313
}
1414
}
15-
int res = 0;
15+
int ans = 0;
1616
for (int i = 0; i < m; ++i) {
17-
if (rows[i] == 1) {
18-
for (int j = 0; j < n; ++j) {
19-
if (picture[i][j] == 'B' && cols[j] == 1) {
20-
++res;
21-
break;
22-
}
17+
for (int j = 0; j < n; ++j) {
18+
if (picture[i][j] == 'B' && rows[i] == 1 && cols[j] == 1) {
19+
++ans;
2320
}
2421
}
2522
}
26-
return res;
23+
return ans;
2724
}
2825
};
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
1-
func findLonelyPixel(picture [][]byte) int {
2-
m, n := len(picture), len(picture[0])
3-
rows := make([]int, m)
4-
cols := make([]int, n)
5-
for i := 0; i < m; i++ {
6-
for j := 0; j < n; j++ {
7-
if picture[i][j] == 'B' {
1+
func findLonelyPixel(picture [][]byte) (ans int) {
2+
rows := make([]int, len(picture))
3+
cols := make([]int, len(picture[0]))
4+
for i, row := range picture {
5+
for j, x := range row {
6+
if x == 'B' {
87
rows[i]++
98
cols[j]++
109
}
1110
}
1211
}
13-
res := 0
14-
for i := 0; i < m; i++ {
15-
if rows[i] == 1 {
16-
for j := 0; j < n; j++ {
17-
if picture[i][j] == 'B' && cols[j] == 1 {
18-
res++
19-
break
20-
}
12+
for i, row := range picture {
13+
for j, x := range row {
14+
if x == 'B' && rows[i] == 1 && cols[j] == 1 {
15+
ans++
2116
}
2217
}
2318
}
24-
return res
19+
return
2520
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy