Rotate Array
Input: [1,2,3,4,5,6,7] and 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]
def rotate(nums,k): n = len(nums) k = k % n nums[:] = nums[n-k:] + nums[:n-k] return nums A = [1,2,3,4,5,6,7] print(rotatearray(A,3))理解Python运算符%:取模 - 返回除法的余数