A java solution (use System.arraycopy)


  • 0
    H

    public class Solution {
    public void rotate(int[] nums, int k) {
    int len = nums.length;
    if (k > len) {
    rotate(nums, k % len);
    } else {
    int[] move = new int[k];
    System.arraycopy(nums, len - k, move, 0, k);
    System.arraycopy(nums, 0, nums, k, len-k);
    System.arraycopy(move, 0, nums, 0, k);
    }
    }
    }


Log in to reply
 

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