Skip to content

Commit

Permalink
Create 0189-rotate-array.java
Browse files Browse the repository at this point in the history
  • Loading branch information
AP-Repositories authored Dec 29, 2022
1 parent e4338a9 commit a1b5494
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions java/0189-rotate-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public void rotate(int[] nums, int k) {
//Do not return anything, modify nums in-place instead
k = k % nums.length;
int l = 0, r = nums.length - 1;
while(l < r) {
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
l += 1;
r -= 1;
}
l = 0;
r = k - 1;
while(l < r) {
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
l += 1;
r -= 1;
}
l = k;
r = nums.length - 1;
while(l < r) {
int tmp = nums[l];
nums[l] = nums[r];
nums[r] = tmp;
l += 1;
r -= 1;
}
}
}

0 comments on commit a1b5494

Please sign in to comment.