当前位置:网站首页>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];
}
}
边栏推荐
- ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
- 最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
- PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
- selenium被反爬了怎么办?
- CodeIgniter 打开错误日志
- 生产力工具和插件
- JSP中如何借助response对象实现页面跳转呢?
- Productivity Tools and Plugins
- sqlalchemy determines whether a field of type array has at least one consistent data with an array
- 技能大赛训练题:域用户和组织单元的创建
猜你喜欢
关于MySQL主从复制的数据同步延迟问题
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
技能大赛训练题:ftp 服务攻防与加固
VU 非父子组件通信
CLion用于STM32开发
Introduction to using NPM
How to quickly split and merge cell data in Excel
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
[Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
C#使用ComboBox控件
随机推荐
IDEA找不到Database解决方法
网络层重点协议——IP协议
Centos7 install mysql5.7
IDEA连接MySQL数据库并执行SQL查询操作
ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
Spark学习:为Spark Sql添加自定义优化规则
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
C# List Usage List Introduction
Centos7 install mysql5.7 steps (graphical version)
为什么 wireguard-go 高尚而 boringtun 孬种
Install the latest pytorch gpu version
AI cocoa AI frontier introduction (7.31)
战略进攻能力的重要性,要远远高于战略防守能力
Solution for browser hijacking by hao360
查看Oracle数据库的用户名和密码
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各种版本