Skip to content

Commit 09bf008

Browse files
committed
feat: add solutions to lc problem: No.2125
No.2125.Number of Laser Beams in a Bank
1 parent f34c9a4 commit 09bf008

File tree

6 files changed

+194
-2
lines changed

6 files changed

+194
-2
lines changed

solution/2100-2199/2125.Number of Laser Beams in a Bank/README.md

+69-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,90 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
直接计数。
71+
7072
<!-- tabs:start -->
7173

7274
### **Python3**
7375

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

7678
```python
77-
79+
class Solution:
80+
def numberOfBeams(self, bank: List[str]) -> int:
81+
last = ans = 0
82+
for b in bank:
83+
if (t := b.count('1')) > 0:
84+
ans += last * t
85+
last = t
86+
return ans
7887
```
7988

8089
### **Java**
8190

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

8493
```java
94+
class Solution {
95+
public int numberOfBeams(String[] bank) {
96+
int last = 0;
97+
int ans = 0;
98+
for (String b : bank) {
99+
int t = 0;
100+
for (char c : b.toCharArray()) {
101+
if (c == '1') {
102+
++t;
103+
}
104+
}
105+
if (t > 0) {
106+
ans += last * t;
107+
last = t;
108+
}
109+
}
110+
return ans;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
int numberOfBeams(vector<string>& bank) {
121+
int ans = 0;
122+
int last = 0;
123+
for (auto& b : bank)
124+
{
125+
int t = 0;
126+
for (char& c : b)
127+
if (c == '1')
128+
++t;
129+
if (t)
130+
{
131+
ans += last * t;
132+
last = t;
133+
}
134+
}
135+
return ans;
136+
}
137+
};
138+
```
85139
140+
### **Go**
141+
142+
```go
143+
func numberOfBeams(bank []string) int {
144+
ans, last := 0, 0
145+
for _, b := range bank {
146+
t := strings.Count(b, "1")
147+
if t > 0 {
148+
ans += t * last
149+
last = t
150+
}
151+
}
152+
return ans
153+
}
86154
```
87155

88156
### **TypeScript**

solution/2100-2199/2125.Number of Laser Beams in a Bank/README_EN.md

+67-1
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,79 @@ This is because the 2<sup>nd</sup> row contains security devices, which breaks t
6161
### **Python3**
6262

6363
```python
64-
64+
class Solution:
65+
def numberOfBeams(self, bank: List[str]) -> int:
66+
last = ans = 0
67+
for b in bank:
68+
if (t := b.count('1')) > 0:
69+
ans += last * t
70+
last = t
71+
return ans
6572
```
6673

6774
### **Java**
6875

6976
```java
77+
class Solution {
78+
public int numberOfBeams(String[] bank) {
79+
int last = 0;
80+
int ans = 0;
81+
for (String b : bank) {
82+
int t = 0;
83+
for (char c : b.toCharArray()) {
84+
if (c == '1') {
85+
++t;
86+
}
87+
}
88+
if (t > 0) {
89+
ans += last * t;
90+
last = t;
91+
}
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int numberOfBeams(vector<string>& bank) {
104+
int ans = 0;
105+
int last = 0;
106+
for (auto& b : bank)
107+
{
108+
int t = 0;
109+
for (char& c : b)
110+
if (c == '1')
111+
++t;
112+
if (t)
113+
{
114+
ans += last * t;
115+
last = t;
116+
}
117+
}
118+
return ans;
119+
}
120+
};
121+
```
70122
123+
### **Go**
124+
125+
```go
126+
func numberOfBeams(bank []string) int {
127+
ans, last := 0, 0
128+
for _, b := range bank {
129+
t := strings.Count(b, "1")
130+
if t > 0 {
131+
ans += t * last
132+
last = t
133+
}
134+
}
135+
return ans
136+
}
71137
```
72138

73139
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int numberOfBeams(vector<string>& bank) {
4+
int ans = 0;
5+
int last = 0;
6+
for (auto& b : bank)
7+
{
8+
int t = 0;
9+
for (char& c : b)
10+
if (c == '1')
11+
++t;
12+
if (t)
13+
{
14+
ans += last * t;
15+
last = t;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func numberOfBeams(bank []string) int {
2+
ans, last := 0, 0
3+
for _, b := range bank {
4+
t := strings.Count(b, "1")
5+
if t > 0 {
6+
ans += t * last
7+
last = t
8+
}
9+
}
10+
return ans
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int numberOfBeams(String[] bank) {
3+
int last = 0;
4+
int ans = 0;
5+
for (String b : bank) {
6+
int t = 0;
7+
for (char c : b.toCharArray()) {
8+
if (c == '1') {
9+
++t;
10+
}
11+
}
12+
if (t > 0) {
13+
ans += last * t;
14+
last = t;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def numberOfBeams(self, bank: List[str]) -> int:
3+
last = ans = 0
4+
for b in bank:
5+
if (t := b.count('1')) > 0:
6+
ans += last * t
7+
last = t
8+
return ans

0 commit comments

Comments
 (0)