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

Commitca4b81a

Browse files
committed
Create 280-wiggle-sort.kt
1 parente8fe5f0 commitca4b81a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎kotlin/280-wiggle-sort.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
classSolution {
2+
funwiggleSort(nums:IntArray):Unit {
3+
for (iin1.. nums.lastIndex) {
4+
val prev= nums[i-1]
5+
val curr= nums[i]
6+
7+
when (i%2) {
8+
0-> {if (prev< curr) nums.swap(i) }
9+
else-> {if (prev> curr) nums.swap(i) }
10+
}
11+
}
12+
}
13+
14+
privatefun IntArray.swap(currentIndex:Int) {
15+
this[currentIndex-1]=this[currentIndex].also {
16+
this[currentIndex]=this[currentIndex-1]
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp