Skip to content

Commit a73b0d4

Browse files
add 189
1 parent a8ea181 commit a73b0d4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,34 @@ fun maximumTripletValue(nums: IntArray): Long {
178178
return res
179179
}
180180

181+
/**
182+
* 189. Rotate Array
183+
*/
184+
185+
fun rotate(nums: IntArray, k: Int): Unit {
186+
if (k % nums.size != 0) {
187+
val cache = mutableListOf<Int>()
188+
val bound = when {
189+
k > nums.size -> {
190+
val temp = k % nums.size
191+
nums.size - temp
192+
}
193+
194+
else -> nums.size - k
195+
}
196+
for (i in bound until nums.size) {
197+
cache.add(nums[i])
198+
}
199+
200+
for (i in 0 until bound) {
201+
cache.add(nums[i])
202+
}
181203

204+
for (i in 0 until cache.size) {
205+
nums[i] = cache[i]
206+
}
207+
}
208+
}
182209

183210

184211

0 commit comments

Comments
 (0)