Leetcode Rotate Array
来源:互联网 发布:铃声mac版 编辑:程序博客网 时间:2024/06/09 15:27
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]
.
数组的旋转问题,需要找到每一个位置对应旋转之后的位置,保存为 itoj 数组,然后需要申请与nums相同的向量空间,赋值就可以了
class Solution {public: void rotate(vector<int>& nums, int k) { vector<int > result(nums.size(),0); vector<int > itoj; if(k<=0) return ; for(int i=0; i<nums.size(); i++) { itoj.push_back((i + k) % nums.size()); } for(int j=0; j<nums.size();j++) { result[itoj[j]] = nums[j]; } nums = result; }};
Submission Result: Accepted
0 0
- [leetcode][array] Rotate Array
- LeetCode[Array]: Rotate Image
- LeetCode:Rotate Array
- [LeetCode] Rotate Array
- *LeetCode-Rotate Array
- [LeetCode]189.Rotate Array
- Rotate Array - LeetCode - Java
- Rotate Array--Leetcode(Java)
- Leetcode--Rotate Array
- LeetCode: Rotate Array
- LeetCode Rotate Array
- Rotate Array(leetcode)
- leetcode Rotate Array
- Leetcode: Rotate Array
- [Leetcode]11 Rotate Array
- [LeetCode][189][Rotate Array]
- Leetcode:Rotate Array
- LeetCode: Rotate Array
- Java中的初始化顺序
- Eclipse中查看jar包中的源码
- 排它平方数
- SQL limit offset
- extern 用法总结
- Leetcode Rotate Array
- Excel操作
- 用fail2ban来防止暴力破解FTP/SSH等服务
- Scheme 语言概要
- android 中对 gps 默认关闭+语言默认中文的设置+时区设置+修改系统名字+修改系统默认时间+修改屏幕休眠时间+修改usb debug默认关闭+mtk修改默认字体大小
- 分辨率和屏幕密度、Display和DisplayMetrics的区别
- Ubuntu Alternate Server与 Desktop之间的区别
- android reboot 功能的添加
- Android中Intent与Bundle 在传值时有什么不同