当前位置:网站首页>牛客网:旋转数组
牛客网:旋转数组
2022-06-25 11:10:00 【lsgoose】

这题是用数组翻转做的,是不是没想到...
挺神奇的,从题目的例子来看:
123456翻转整个变成654321
然后我们翻转前2位就变成564321,翻转后4位就变成了561234,是不是很神奇....
一开始我还想着用循环移位来做...
答案如下所示:
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) {
m = m%n;
reverse(a.begin(), a.end());
reverse(a.begin(), a.begin()+m);
reverse(a.begin()+m, a.end());
return a;
}
};边栏推荐
- Explanation and use of kotlin syntax for Android
- Kingbasees plug-in DBMS of Jincang database_ RANDOM
- 一个数学难题,难倒两位数学家
- [维护集群案例集] GaussDB 查询用户空间使用情况
- Apache ShenYu 入门
- Redis6笔记02 配置文件,发布和订阅,新数据类型,Jedis操作
- 仿真与烧录程序有哪几种方式?(包含常用工具与使用方式)
- SystemVerilog(十三)-枚举数据类型
- Garbage collection mechanism
- SystemVerilog (XIII) - enumerate data types
猜你喜欢
随机推荐
GC
A program reflecting the characteristics of C language program structure
C disk uses 100% cleaning method
Compilation of learning from Wang Shuang (1)
Jincang KFS data centralized scenario (many to one) deployment
Five types of questions about network planning
Shen Lu, China Communications Institute: police open source Protocol - ofl v1.1 Introduction and Compliance Analysis
金仓数据库 KingbaseES 插件DBMS_UTILITY
Open source invites you to participate in the openssf Open Source Security Online Seminar
Free access to the global human settlements layer (ghsl) dataset from Gee
SystemVerilog(十三)-枚举数据类型
Crawler scheduling framework of scratch+scratch+grammar
中國信通院沈瀅:字體開源協議——OFL V1.1介紹及合規要點分析
relu与sigmod的比较
Network remote access using raspberry pie
Jincang database kingbasees plug-in force_ view
Spannable and editable, spannablestring and spannablestring
Handler、Message、Looper、MessageQueue
Handler、Message、Looper、MessageQueue
子类A继承父类B, A a = new A(); 则父类B构造函数、父类B静态代码块、父类B非静态代码块、子类A构造函数、子类A静态代码块、子类A非静态代码块 执行的先后顺序是?









