Skip to content

Commit 4acd838

Browse files
authored
Create 0189-rotate-array.kt
1 parent 8ae983c commit 4acd838

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

kotlin/0189-rotate-array.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
fun rotate(nums: IntArray, k: Int): Unit {
3+
4+
fun reverse(from: Int, to: Int) {
5+
var f = from
6+
var t = to
7+
while (f < t) {
8+
nums[f] = nums[t].also { nums[t--] = nums[f++] }
9+
}
10+
}
11+
12+
val modK = (k % nums.size)
13+
reverse(0, nums.lastIndex)
14+
reverse(0, modK - 1)
15+
reverse(modK, nums.lastIndex)
16+
}
17+
}

0 commit comments

Comments
 (0)