Skip to content

Instantly share code, notes, and snippets.

@dmnugent80
Created March 1, 2015 00:43
Show Gist options
  • Select an option

  • Save dmnugent80/7ff4cd0d6ef047ac0946 to your computer and use it in GitHub Desktop.

Select an option

Save dmnugent80/7ff4cd0d6ef047ac0946 to your computer and use it in GitHub Desktop.
Rotate Array In-Place
public class Solution {
public void rotate(int[] nums, int k) {
if (k == 0) return;
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[] arr, int i, int j){
while (i < j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment