rotate-array.dart (730B)
1 //Given an array rotate it k times to the right. 2 //To solve this I first take the remainder of K / nums.length 3 //then create another list that is the sublists of nums so that it stores 4 //the roatated values. Then for each value in the list they are copied to the original list. 5 //The time complexity of this code is O(n) where n is the length of the nums list. 6 //Time: 326ms Beats: 49.18% 7 //Memory: 163.9MB Beats: 8.20% 8 9 class Solution { 10 void rotate(List<int> nums, int k) { 11 List<int> new_list = []; 12 k = k % nums.length; 13 new_list = nums.sublist(nums.length - k); 14 new_list += nums.sublist(0, nums.length - k); 15 for(int i = 0 ; i < new_list.length ; ++i){ 16 nums[i] = new_list[i]; 17 } 18 } 19 }