当前位置:网站首页>LeetCode旋转数组
LeetCode旋转数组
2022-07-31 13:26:00 【超爱学习芸】
给你一个数组,将数组中的元素向右轮转 k
个位置,其中 k
是非负数。
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
输入:nums = [-1,-100,3,99], k = 2
输出:[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]
解题思路:
创建一个新数组,number[(i+k)%numsSize]=nums[i]
再赋值给新数组
代码展示:
void rotate(int* nums, int numsSize, int k){
int number[numsSize];
for(int i=0;i<numsSize;i++){
number[(i+k)%numsSize]=nums[i];
}
for(int i=0;i<numsSize;i++){
nums[i]=number[i];
}
}
边栏推荐
- P5019 [NOIP2018 提高组] 铺设道路
- IDEA的database使用教程(使用mysql数据库)
- The importance of strategic offensive capability is much higher than strategic defensive capability
- mysql8, starttime的下一个值作为endtime的上一个值?
- 求一份常见Oracle故障模拟场景
- All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- 0X7FFFFFFF,0X80000000「建议收藏」
- 拥塞控制,CDN,端到端
- 聊聊 SAP 产品 UI 上的消息显示机制
- 基于高阶微分器的无模型滑模控制器及其在自动电压调节器中的应用
猜你喜欢
随机推荐
Centos7 install mysql5.7
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
报错:npm ERR code EPERM
networkx绘制度分布
ECCV2022: Recursion on Transformer without adding parameters and less computation!
机器学习模型验证:被低估的重要一环
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
/run/NetworkManager占用空间过大
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
golang中使用泛型
Six Stones Programming: No matter which function you think is useless, people who can use it will not be able to leave, so at least 99%
生产力工具和插件
The operator,
Save and load numpy matrices and vectors, and use the saved vectors for similarity calculation
CentOS7 installation MySQL graphic detailed tutorial
The latest complete code: Incremental training using the word2vec pre-training model (two loading methods corresponding to two saving methods) applicable to various versions of gensim
最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
页面整屏滚动效果
golang-gin - graceful restart