leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 3b43f9335fe6c854605c51d7146521ef65c60ea5
parent b284a4bfdb97fdf2da5d6746ee5f231a0eb66ea6
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sun,  7 May 2023 16:03:28 -0500

Completed rotate array problem using dart

Diffstat:
Arotate-array/rotate-array.dart | 19+++++++++++++++++++
1 file changed, 19 insertions(+), 0 deletions(-)

diff --git a/rotate-array/rotate-array.dart b/rotate-array/rotate-array.dart @@ -0,0 +1,19 @@ +//Given an array rotate it k times to the right. +//To solve this I first take the remainder of K / nums.length +//then create another list that is the sublists of nums so that it stores +//the roatated values. Then for each value in the list they are copied to the original list. +//The time complexity of this code is O(n) where n is the length of the nums list. +//Time: 326ms Beats: 49.18% +//Memory: 163.9MB Beats: 8.20% + +class Solution { + void rotate(List<int> nums, int k) { + List<int> new_list = []; + k = k % nums.length; + new_list = nums.sublist(nums.length - k); + new_list += nums.sublist(0, nums.length - k); + for(int i = 0 ; i < new_list.length ; ++i){ + nums[i] = new_list[i]; + } + } +}