LeetCode---Rotate Array
来源:互联网 发布:淘宝店铺的层级和名次 编辑:程序博客网 时间:2024/06/06 20:46
题目大意:给出一个数组和一个数K,要求向右旋转数组k个位置,返回修改后的数组。
算法思想:
1.获取数组的大小n,如果数组为空直接返回。
2,设置一个大小为n缓冲数组,用来记录数组旋转后的结果。
3.利用STL中的rotate_copy算法将旋转后的结果放置在缓冲数组中。
4.将缓冲数组中的结果,赋值到原数组对应的位置。
代码如下:
class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); if(n==0) return; vector<int> temp(n); if(k>=n) k=k%n; rotate_copy(nums.begin(),nums.end()-k,nums.end(),temp.begin()); for(int i=0;i<n;++i){ nums[i]=temp[i]; } }};
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
- 非常好的websocket讲解2
- 【UUIKit-51-1】UIImageView-创建与属性
- 【Docker入门】使用Nginx反向代理最前端,多个Docker容器做后端。将多台服务器整合到一台服务器上
- proxychains and ssh(代理服务器linux)
- Cognos学习笔记(一)
- LeetCode---Rotate Array
- 初学shell,今天遇到由wget下载到本地的网页源代码的乱码问题,无聊的写了一个转码的脚本
- ssoj2457备用钥匙
- 网站开发进阶(五)网站发布之道
- IOS网络笔记--地图内容4(定位到当前位置)
- 队列即基本操作(c链表实现)
- 微信支付的坑
- iOS经典讲解之在navigationBar上面添加多个任意控件
- 今天回归啦