# Easy to read Java solution

• I really don't like those something little line solutions as they are incredibly hard to read. Below is my solution.

``````public void rotate(int[] nums, int k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
}

public void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}``````

• I have a similar solution. Though long but very easy to understand...

``````class Solution {
public:
void rotate(vector<int>& nums, int k) {
if(!nums.size()) return;
k %= nums.size();
// well, it depends on left rotate or right rotate
// in this case, its right rotate so need to do a simple math
k = nums.size()-k;
int temp;
// reflect [0,k-1], [k,size()-1]
// then reflect the whole array
// e.g. [1,2,|3,4] --> [2,1,|4,3] --> [3,4,1,2]
int l = 0, r = k-1;
while(l<r) {
temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++,r--;
}
l = k, r = nums.size()-1;
while(l<r) {
temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++,r--;
}
l = 0, r = nums.size()-1;
while(l<r) {
temp = nums[l];
nums[l] = nums[r];
nums[r] = temp;
l++,r--;
}
}
};``````

• very good solution, thanks

• Clear and make sense. Thanks!

• @danny6514 very concise solution!!! but could you explain the main idea behind it? thank you so much.

• People are not so nice...

• poor guy....

• This post is deleted!

• ``````public class Solution {
public void rotate(int[] nums, int k) {
int n = nums.length;
if(k==n) return;
if(k==0) return;
if(n<=1) return;

int start = 0;
int swapPos = k%n;
int temp;
for(int step=0;step<n;step++){
if(swapPos==start){
start++;
swapPos = (start+k)%n;
continue;
}
temp = nums[start];
nums[start] = nums[swapPos];
nums[swapPos] = temp;
swapPos = (swapPos+k)%n;
}
}
``````

}

• It's legit and it's amazing. but I can't figure out why it's legit.

• can someone explain this solution .

• let a= [1,2,3,4,5,6,7]
k = 3.

1. we have to first reverse the whole array by swapping first element with the last one and so on..
you will get[7,6,5,4,3,2,1]

2. reverse the elements from 0 to k-1
reverse the elements 7,6,5
you will get [5,6,7,4,3,2,1]

3. reverse the elements from k to n-1
reverse the elements 4,3,2,1
you will get[5,6,7,1,2,3,4]

• this is so smart

• this problem is from the book of "beauty of programming"

• elegant solution, thank you!

• I don't understand the role of k%=nums.length. Doesn't it change the value of k? I tried without it and i get index out of bound in the reverse function.

• @sonakshi4 This is because that sometimes, k is larger than the length of array. For example nums = [1, 2, 3, 4, 5], k = 12, this means we only need to rotate the last two numbers. k = k % nums.length = 2;

• @junpitt I didnt think of that possibility. Thank you for replying :)

• legit

I am going to explain this with a simple example. Say with n = 7 and k = 3, the array = [1,2,3,4,5,6,7].

1. Reverse the whole array, so array = [7,6,5,4,3,2,1]
2. Reverse the first k elements, so array = [5,6,7,4,3,2,1]
3. Reverse [k+1, n] elements, so array = [5,6,7,1,2,3,4]

• niubi...thx~~~~~

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.