[leetcode: Python]189. Rotate Array
来源:互联网 发布:海尔软件待遇怎么样 编辑:程序博客网 时间:2024/06/06 00:14
题目:
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].
Note:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
注意:
尽量想出三种方法。
方法一:性能238ms
class Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: void Do not return anything, modify nums in-place instead. """ while k > 0: nums.insert(0,nums.pop()) k -= 1
方法二:性能75ms
class Solution(object): def rotate(self, nums, k): nums_copy = nums[:] l = len(nums) for i, n in enumerate(nums_copy): nums[(i + k) % l] = n
方法三:性能66ms
class Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: void Do not return anything, modify nums in-place instead. """ nums.reverse() k%=len(nums) nums[:k]=nums[:k][::-1] nums[k:]=nums[k:][::-1]
0 0
- [leetcode: Python]189. Rotate Array
- 【leetcode】189. Rotate Array(Python & C++)
- [LeetCode]189.Rotate Array
- [Leetcode] 189.Rotate Array
- LeetCode 189. Rotate Array
- 【LeetCode】189.Rotate Array
- [leetcode] 189.Rotate Array
- 【leetcode】189. Rotate Array
- Leetcode 189. Rotate Array
- 189. Rotate Array LeetCode
- [LeetCode]189. Rotate Array
- leetcode-189.-Rotate Array
- LeetCode 189. Rotate Array
- leetcode 189. Rotate Array
- LeetCode *** 189. Rotate Array
- [LeetCode]189. Rotate Array
- LeetCode 189. Rotate Array
- leetcode-189. Rotate Array
- unity3d摄像机
- iOS获取手机系统版本
- Chrome浏览器插件postman下载
- 《计算机网络》谢希仁(第7版) 第一章
- php基础知识 6 自定义函数
- [leetcode: Python]189. Rotate Array
- 移动web双指放大问题,vue滑动被禁止的问题
- SpringMVC与velocity集成
- IOS 静态(常/变)量的 初始化方法
- ITween要点记录1-指定方法
- 欢迎使用CSDN-markdown编辑器
- 理解RESTful架构
- 前端必读:浏览器内部工作原理
- WHAT DOES “NEURONS THAT FIRE TOGETHER WIRE TOGETHER” MEAN?