Skip to content

Commit 4f1c893

Browse files
committed
feat: add solutions to lc problem: No.0531.Lonely Pixel I
1 parent 304b0aa commit 4f1c893

File tree

6 files changed

+334
-4
lines changed

6 files changed

+334
-4
lines changed

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

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
<strong>输出:</strong> 3
2323
<strong>解析:</strong> 全部三个&#39;B&#39;都是黑色孤独像素。
24+
25+
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0531.Lonely%20Pixel%20I/images/pixel1.jpg" style="width: 242px; height: 242px;" />
2426
</pre>
2527

2628
<p>&nbsp;</p>
@@ -33,27 +35,140 @@
3335

3436
<p>&nbsp;</p>
3537

36-
3738
## 解法
3839

3940
<!-- 这里可写通用的实现逻辑 -->
4041

42+
数组或哈希表统计每一行、每一列中 'B' 出现的次数。
43+
4144
<!-- tabs:start -->
4245

4346
### **Python3**
4447

4548
<!-- 这里可写当前语言的特殊实现逻辑 -->
4649

4750
```python
48-
51+
class Solution:
52+
def findLonelyPixel(self, picture: List[List[str]]) -> int:
53+
m, n = len(picture), len(picture[0])
54+
rows, cols = [0] * m, [0] * n
55+
for i in range(m):
56+
for j in range(n):
57+
if picture[i][j] == 'B':
58+
rows[i] += 1
59+
cols[j] += 1
60+
res = 0
61+
for i in range(m):
62+
if rows[i] == 1:
63+
for j in range(n):
64+
if picture[i][j] == 'B' and cols[j] == 1:
65+
res += 1
66+
break
67+
return res
4968
```
5069

5170
### **Java**
5271

5372
<!-- 这里可写当前语言的特殊实现逻辑 -->
5473

5574
```java
75+
class Solution {
76+
public int findLonelyPixel(char[][] picture) {
77+
int m = picture.length, n = picture[0].length;
78+
int[] rows = new int[m];
79+
int[] cols = new int[n];
80+
for (int i = 0; i < m; ++i) {
81+
for (int j = 0; j < n; ++j) {
82+
if (picture[i][j] == 'B') {
83+
++rows[i];
84+
++cols[j];
85+
}
86+
}
87+
}
88+
int res = 0;
89+
for (int i = 0; i < m; ++i) {
90+
if (rows[i] == 1) {
91+
for (int j = 0; j < n; ++j) {
92+
if (picture[i][j] == 'B' && cols[j] == 1) {
93+
++res;
94+
break;
95+
}
96+
}
97+
}
98+
}
99+
return res;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int findLonelyPixel(vector<vector<char>>& picture) {
110+
int m = picture.size(), n = picture[0].size();
111+
vector<int> rows(m);
112+
vector<int> cols(n);
113+
for (int i = 0; i < m; ++i)
114+
{
115+
for (int j = 0; j < n; ++j)
116+
{
117+
if (picture[i][j] == 'B')
118+
{
119+
++rows[i];
120+
++cols[j];
121+
}
122+
}
123+
}
124+
int res = 0;
125+
for (int i = 0; i < m; ++i)
126+
{
127+
if (rows[i] == 1)
128+
{
129+
for (int j = 0; j < n; ++j)
130+
{
131+
if (picture[i][j] == 'B' && cols[j] == 1)
132+
{
133+
++res;
134+
break;
135+
}
136+
}
137+
}
138+
}
139+
return res;
140+
}
141+
};
142+
```
56143
144+
### **Go**
145+
146+
```go
147+
func findLonelyPixel(picture [][]byte) int {
148+
m, n := len(picture), len(picture[0])
149+
rows := make([]int, m)
150+
cols := make([]int, n)
151+
for i := 0; i < m; i++ {
152+
for j := 0; j < n; j++ {
153+
if picture[i][j] == 'B' {
154+
rows[i]++
155+
cols[j]++
156+
}
157+
}
158+
}
159+
res := 0
160+
for i := 0; i < m; i++ {
161+
if rows[i] == 1 {
162+
for j := 0; j < n; j++ {
163+
if picture[i][j] == 'B' && cols[j] == 1 {
164+
res++
165+
break
166+
}
167+
}
168+
}
169+
}
170+
return res
171+
}
57172
```
58173

59174
### **...**

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

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,132 @@
3434
<li><code>picture[i][j]</code> is <code>&#39;W&#39;</code> or <code>&#39;B&#39;</code>.</li>
3535
</ul>
3636

37-
3837
## Solutions
3938

4039
<!-- tabs:start -->
4140

4241
### **Python3**
4342

4443
```python
45-
44+
class Solution:
45+
def findLonelyPixel(self, picture: List[List[str]]) -> int:
46+
m, n = len(picture), len(picture[0])
47+
rows, cols = [0] * m, [0] * n
48+
for i in range(m):
49+
for j in range(n):
50+
if picture[i][j] == 'B':
51+
rows[i] += 1
52+
cols[j] += 1
53+
res = 0
54+
for i in range(m):
55+
if rows[i] == 1:
56+
for j in range(n):
57+
if picture[i][j] == 'B' and cols[j] == 1:
58+
res += 1
59+
break
60+
return res
4661
```
4762

4863
### **Java**
4964

5065
```java
66+
class Solution {
67+
public int findLonelyPixel(char[][] picture) {
68+
int m = picture.length, n = picture[0].length;
69+
int[] rows = new int[m];
70+
int[] cols = new int[n];
71+
for (int i = 0; i < m; ++i) {
72+
for (int j = 0; j < n; ++j) {
73+
if (picture[i][j] == 'B') {
74+
++rows[i];
75+
++cols[j];
76+
}
77+
}
78+
}
79+
int res = 0;
80+
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+
}
87+
}
88+
}
89+
}
90+
return res;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int findLonelyPixel(vector<vector<char>>& picture) {
101+
int m = picture.size(), n = picture[0].size();
102+
vector<int> rows(m);
103+
vector<int> cols(n);
104+
for (int i = 0; i < m; ++i)
105+
{
106+
for (int j = 0; j < n; ++j)
107+
{
108+
if (picture[i][j] == 'B')
109+
{
110+
++rows[i];
111+
++cols[j];
112+
}
113+
}
114+
}
115+
int res = 0;
116+
for (int i = 0; i < m; ++i)
117+
{
118+
if (rows[i] == 1)
119+
{
120+
for (int j = 0; j < n; ++j)
121+
{
122+
if (picture[i][j] == 'B' && cols[j] == 1)
123+
{
124+
++res;
125+
break;
126+
}
127+
}
128+
}
129+
}
130+
return res;
131+
}
132+
};
133+
```
51134
135+
### **Go**
136+
137+
```go
138+
func findLonelyPixel(picture [][]byte) int {
139+
m, n := len(picture), len(picture[0])
140+
rows := make([]int, m)
141+
cols := make([]int, n)
142+
for i := 0; i < m; i++ {
143+
for j := 0; j < n; j++ {
144+
if picture[i][j] == 'B' {
145+
rows[i]++
146+
cols[j]++
147+
}
148+
}
149+
}
150+
res := 0
151+
for i := 0; i < m; i++ {
152+
if rows[i] == 1 {
153+
for j := 0; j < n; j++ {
154+
if picture[i][j] == 'B' && cols[j] == 1 {
155+
res++
156+
break
157+
}
158+
}
159+
}
160+
}
161+
return res
162+
}
52163
```
53164

54165
### **...**
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int findLonelyPixel(vector<vector<char>>& picture) {
4+
int m = picture.size(), n = picture[0].size();
5+
vector<int> rows(m);
6+
vector<int> cols(n);
7+
for (int i = 0; i < m; ++i)
8+
{
9+
for (int j = 0; j < n; ++j)
10+
{
11+
if (picture[i][j] == 'B')
12+
{
13+
++rows[i];
14+
++cols[j];
15+
}
16+
}
17+
}
18+
int res = 0;
19+
for (int i = 0; i < m; ++i)
20+
{
21+
if (rows[i] == 1)
22+
{
23+
for (int j = 0; j < n; ++j)
24+
{
25+
if (picture[i][j] == 'B' && cols[j] == 1)
26+
{
27+
++res;
28+
break;
29+
}
30+
}
31+
}
32+
}
33+
return res;
34+
}
35+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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' {
8+
rows[i]++
9+
cols[j]++
10+
}
11+
}
12+
}
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+
}
21+
}
22+
}
23+
}
24+
return res
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int findLonelyPixel(char[][] picture) {
3+
int m = picture.length, n = picture[0].length;
4+
int[] rows = new int[m];
5+
int[] cols = new int[n];
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
if (picture[i][j] == 'B') {
9+
++rows[i];
10+
++cols[j];
11+
}
12+
}
13+
}
14+
int res = 0;
15+
for (int i = 0; i < m; ++i) {
16+
if (rows[i] == 1) {
17+
for (int j = 0; j < n; ++j) {
18+
if (picture[i][j] == 'B' && cols[j] == 1) {
19+
++res;
20+
break;
21+
}
22+
}
23+
}
24+
}
25+
return res;
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def findLonelyPixel(self, picture: List[List[str]]) -> int:
3+
m, n = len(picture), len(picture[0])
4+
rows, cols = [0] * m, [0] * n
5+
for i in range(m):
6+
for j in range(n):
7+
if picture[i][j] == 'B':
8+
rows[i] += 1
9+
cols[j] += 1
10+
res = 0
11+
for i in range(m):
12+
if rows[i] == 1:
13+
for j in range(n):
14+
if picture[i][j] == 'B' and cols[j] == 1:
15+
res += 1
16+
break
17+
return res

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