We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a8ea181 commit a73b0d4Copy full SHA for a73b0d4
contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
@@ -178,7 +178,34 @@ fun maximumTripletValue(nums: IntArray): Long {
178
return res
179
}
180
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
202
203
204
+ for (i in 0 until cache.size) {
205
+ nums[i] = cache[i]
206
207
208
+}
209
210
211
0 commit comments