I'm trying to complete the 189. Rotate Array problem on Leetcode. This is the code I've written:
class Solution(object):
def rotate(self, nums, k):
end = len(nums) - 1
carry = []
new = [n for n in nums]
for i in range(end-k 1, end 1):
carry.append(nums[i])
for i in range(1, k 1):
new.pop()
for n in new:
carry.append(n)
print(carry)
return carry
(The obsolete variables were to try and erase any possibility of something wacky happening with Leetcode's testing system.)
I can run this code anywhere (I've tried VS Code and an online interpreter) and the result is always correct (for the first case, [5, 6, 7, 1, 2, 3, 4]). However, when I try in Leetcode:
The stdout shows the correct answer literally the line before the return, and yet the array somehow magically changes through no tampering.
This is driving me up the wall... please help!!
(Problem description:
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Constraints:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
Follow up:
Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
Could you do it in-place with O(1) extra space?
)
CodePudding user response:
The Python template for this problem says:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
As the docstring mentioned, you'll need to modify nums
in-place. There is no need to print or return anything.
A simple fix to your existing code would be to use an assignment nums[:] = carry
at the end.
CodePudding user response:
To answer @mreyeball's question about why nums = carry
doesn't work:
Lists are passed by reference. This means that if you pass a list to a method, the list inside the method will refer to the same memory location as the list outside of the method.
This means that changes inside the method will affect the list outside the method as well.
However, this only goes for changes to the elements of a list. When you do nums = carry
, you are literally saying "The variable nums
will now refer to the same list as carry
". Therefore, there is no longer a link between nums
and the array that was originally passed to the method.
foo = [1,2,3,4,5]
doStuff(foo)
...
def doStuff(arr):
arr[0] = 2 # this will affect 'foo' as well
bar = ['a','b','c'] # new array
arr = bar # link broken!
arr[0] = 'd' # this won't affect 'foo', but it will affect 'bar'.