这是我再 leetcode 上遇见的一道题: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
我是这样完成的
# @param {Integer[]} nums
# @param {Integer} k
# @return {void} Do not return anything, modify nums in-place instead.
def rotate(nums, k)
k %=nums.length
if k!= 0
t = nums[k..(nums.length-1).to_i]
f = nums[0..(k-1).to_i]
nums = t + f
end
end
问题是 nums 的值只在函数内改变了