O(n) Eight lines Java solution


  • -5
    N
    public void rotate(int[] nums, int k) {
            int[] res = new int[nums.length];
            for(int i=0;i<nums.length;i++){
                int offset = k%nums.length;
                res[i] = nums[(i+nums.length-offset)%(nums.length)];
            }
            for(int i=0;i<nums.length;i++){
               nums[i] = res[i];
            }
        }

  • 0
    J
    This post is deleted!

Log in to reply
 

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