Skip to content

Commit 045b916

Browse files
Create 2125-number-of-laser-beams-in-a-bank.java
1 parent 46efed6 commit 045b916

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int numberOfBeams(String[] bank) {
3+
int prev = countOnes(bank[0]);
4+
int res = 0;
5+
6+
for(int i = 1; i < bank.length; i++){
7+
int curr = countOnes(bank[i]);
8+
if(curr > 0){
9+
res += curr*prev;
10+
prev = curr;
11+
}
12+
}
13+
return res;
14+
}
15+
private int countOnes(String str){
16+
int res = 0;
17+
for(char c: str.toCharArray())
18+
res += (c == '1')? 1: 0;
19+
return res;
20+
}
21+
}

0 commit comments

Comments
 (0)