Beats 98% of Java Soutions


  • 4
    A
    public class Solution {
        public void rotate(int[] nums, int k) {
            int n = nums.length;
            int[] newList = new int[n];
            if (n > 1) {
                k = k % n;
                System.arraycopy(nums, n - k, newList, 0, k);
                System.arraycopy(nums, 0, newList, k, n - k);
                System.arraycopy(newList, 0, nums, 0, n);
            }
        }
    }

Log in to reply
 

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