Solution with ArrayList


  • 0
    T
    public void rotate(int[] nums, int k) {
        
        ArrayList<Integer> a = new ArrayList<>();
        
        k = k%nums.length;
        
        if (k == 0 || nums.length <= 1) {
            return;
        }
        
        int count = 0;
        
        for (int h= nums.length-k; h<= nums.length-1; h++ ) {
            a.add(nums[h]);
        }
        
        while (!a.isEmpty() && count< nums.length) {
            a.add(nums[count]);
            nums[count] = a.get(count++);
        }
        
    }

Log in to reply
 

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