A Java in-place solution easy to understand


  • 0
    M

    say we rotate array a = [1,2,3,4,5,6,7] by 3 steps
    first reverse the array: a = [7,6,5,4,3,2,1]
    then reverse the first part ( at index 0 to k-1(in this case 2) ) a = [5,6,7, ... ]
    finally reverse the rest (at index k to the last) a = [..., 1,2,3,4]

    public class Solution {
        public void rotate(int[] nums, int k) {
            k %= nums.length;
            nums = rotate(nums, 0, nums.length-1);
            nums = rotate(nums, 0, k-1);
            nums = rotate(nums, k, nums.length-1);
        }
        private int[] rotate(int[] nums, int head, int tail){
            int i = head, j = tail;
            int temp;
            while( i <= j){
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
                i++;
                j--;
            }
            return nums;
        }
    }

  • 0
    C

    I like this solution but you don't need to return an int[]. Java passes the value of the reference for arrays.


Log in to reply
 

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