Move k items as one block


  • 0
    M
    public void rotate(int[] nums, int k) {
        /*The First solution*/
        int size = nums.length;
        k %= size;
        if(k>0){
            int[] temp = new int[k];  //define the block for k items
            for(int i=0;i<k; i++){
                temp[i] = nums[size-k+i];
            }
            for(int j=0; j<size-k; j++){
                nums[size-j-1] = nums[size-k-j-1];
            }
            for(int t=0; t<k; t++){
                nums[t] = temp[t];
            }
        }
    }

Log in to reply
 

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