189. Rotate Array
来源:互联网 发布:sql日月年转年月日 编辑:程序博客网 时间:2024/06/05 21:18
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.
注意:k可能大于n
Approach #1
class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); vector<int> tmp(nums);//void int j=0; for(int i=0;i<n;i++) { nums[(i+k)%n]=tmp[i];//移动数据 } }};
时间复杂度:O(n)
空间复杂度:O(n)
Approach #2
每次把最后一个数据往前复制,其他数据往后挪,重复k次阅读全文
0 0
- Leetcode - Array - 189. Rotate Array
- 189. Rotate Array leetcode(array)
- LeetCode-Array-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
- 189. Rotate Array LeetCode
- 189. Rotate Array
- [LeetCode]189. Rotate Array
- 189. Rotate Array
- leetcode-189.-Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- 189. Rotate Array
- HashMap源码--(三)put方法
- Android 小米手机开发APP图标更换后还显示原来的图标
- 优化或修改CentOS最大连接数限制
- CIR,CBS,EBS,PIR,PBS令牌桶概述
- Android RuntimePermissions运行时权限:批量权限申请
- 189. Rotate Array
- quartz 定时任务管理
- Hive
- poj1000-1009小结
- 计算两个经纬度之间的距离,可以用于查看用户与商家之间的距离
- 使用JAVA自带security,创建测试用的CSR证书请求文件
- SAP UI5初步结构分析研究(八)(BarcodeScanner)
- HDU
- response对象在servlet中的常用方法讲解