Question Reference: https://leetcode.com/problems/rotate-array/
I'm working on Leetcode Question 189. Rotate Array and I solved it by studying other coders' solutions. But new to coding, I still have some questions still and hope someone can help clarify these:
#1 Solution - This works:
k %= len(nums)
nums[:] = nums[-k:] + nums[0:-k]
#2 Solution - This doesn't:
k %= len(nums)
nums = nums[-k:] + nums[0:-k]
#gives me the original nums which didn't change
But I tried #2 Solution in some Python online IDE, nums
did change and gave back the same result as #1 Solution. Why didn't it work in Leetcode?
Another question I have is - the question requires "modify nums in-place", which in my understanding is to perform the operation without using any additional memory. But in #1 Solution, we create a shallow copy of nums
by using nums[:]
, does it meet the requirement?
Thanks!