Skip to content

Commit 561a6bd

Browse files
authored
Merge pull request #2298 from yaxarat/kotlin-280
Add 0280-wiggle-sort.kt
2 parents e8fe5f0 + 6ff4ee6 commit 561a6bd

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

kotlin/0280-wiggle-sort.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
fun wiggleSort(nums: IntArray): Unit {
3+
for (i in 1 .. 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+
private fun 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)