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

Commit6561b5d

Browse files
authored
Create 0473-matchsticks-to-square.kt
1 parent6f8f113 commit6561b5d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎kotlin/0473-matchsticks-to-square.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
funmakesquare(matchsticks:IntArray):Boolean {
3+
val length= matchsticks.sum()!!/4
4+
val sides=IntArray (4)
5+
6+
if (matchsticks.sum()!!/4!= length)returnfalse
7+
8+
matchsticks.sortDescending()
9+
10+
funbacktrack(i:Int):Boolean {
11+
if (i== matchsticks.size)returntrue
12+
13+
for (jin0..3) {
14+
if (sides[j]+ matchsticks[i]<= length) {
15+
sides[j]+= matchsticks[i]
16+
if (backtrack(i+1))
17+
returntrue
18+
sides[j]-= matchsticks[i]
19+
}
20+
}
21+
22+
returnfalse
23+
}
24+
25+
return backtrack(0)
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp