题解 | 旋转数组

旋转数组

https://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042

#include <deque>
#include <vector>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 旋转数组
     * @param n int整型 数组长度
     * @param m int整型 右移距离
     * @param a int整型vector 给定数组
     * @return int整型vector
     */
    vector<int> Rotatedarray(vector<int>& a,int s){
        int t;
        for( int k = 0  ; k<s  ;  k++  ){ 
            t = a.back();
            auto i = a.begin();
            a.insert(i,t);
            a.pop_back();
            
        }
        return a;
    }


    vector<int> solve(int n, int m, vector<int>& a) {
        // write code here
        return Rotatedarray(a,m);
    }
};

只要会把数组元素往后推一次,然后循环m次就行

全部评论

相关推荐

点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务