Skip to content

Instantly share code, notes, and snippets.

@rootid
Created May 24, 2015 20:06
Show Gist options
  • Save rootid/ff2d3975d73e308e8f59 to your computer and use it in GitHub Desktop.
Save rootid/ff2d3975d73e308e8f59 to your computer and use it in GitHub Desktop.
public static void reverse (int nums[],int start,int end) {
while(start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start++;
end--;
}
}
public static void rotate(int[] nums, int k) {
int n = nums.length;
k %= n;
reverse(nums,0,n-1);
reverse(nums,0,k-1);
reverse(nums,k,n-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment