题解 | #旋转数组#

旋转数组

https://www.nowcoder.com/practice/e19927a8fd5d477794dac67096862042?tpId=295&tqId=1024689&ru=%2Fpractice%2F4edf6e6d01554870a12f218c94e8a299&qru=%2Fta%2Fformat-top101%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj

原本以为跳着改变元素能够较快完成了已经,没想到这更简洁。。。

class Solution {
public:
    /**
     * 旋转数组
     * @param n int整型 数组长度
     * @param m int整型 右移距离
     * @param a int整型vector 给定数组
     * @return int整型vector
     */
    vector<int> solve(int n, int m, vector<int>& a) {
        // write code here
      m %= n;
      std::reverse(a.begin(), a.end());
      std::reverse(a.begin(), a.begin() + m);
      std::reverse(a.begin() + m, a.end());
      
      return a;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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