Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4a5e9a3

Browse files
authored
Create 2125-number-of-laser-beams-in-a-bank.kt
1 parenta5b00e6 commit4a5e9a3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classSolution {
2+
funnumberOfBeams(bank:Array<String>):Int {
3+
var prev= bank[0].count { it=='1'}
4+
var res=0
5+
6+
for (iin1 until bank.size) {
7+
val cur= bank[i].count { it=='1'}
8+
if (cur>0) {
9+
res+= (prev* cur)
10+
prev= cur
11+
}
12+
}
13+
14+
return res
15+
}
16+
}
17+
18+
// or do it the kotlin way!
19+
classSolution {
20+
funnumberOfBeams(bank:Array<String>)= bank
21+
.map { it.count { it=='1' } }
22+
.filterNot { it==0 }
23+
.zipWithNext { a, b-> a* b }
24+
.sum()
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp