My ArrayList solution without Swap or Reverse


  • 0
    A
    public class Solution {
        public void rotate(int[] nums, int k) 
        {
            List<Integer> temp = new ArrayList<Integer>();
            int i;
            int len = nums.length;
            for (i = 0; i < len; i++)
            {
                temp.add(nums[i]);
            }
            for (i = 0; i < k; i++ )
            {   
                temp.add(0,temp.get(len-1));
                temp.remove(len);
            }
            for (i = 0; i < nums.length; i++)
            {
                nums[i] = temp.get(i);
            }
        }
    }
    

    What's the time complexity and space complexity of this? O(n) and O(n)?


Log in to reply
 

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