当前位置:网站首页>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];
}
}
边栏推荐
- STM32——软件SPI控制AD7705[通俗易懂]
- 求一份常见Oracle故障模拟场景
- The use of C# control CheckBox
- 网络层重点协议——IP协议
- 报错:npm ERR code EPERM
- Solution for browser hijacking by hao360
- Selenium IDE for Selenium Automation Testing
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
- EXCEL如何快速拆分合并单元格数据
- 六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
猜你喜欢
Spark学习:为Spark Sql添加自定义优化规则
C#使用NumericUpDown控件
How IDEA runs web programs
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
The batch size does not have to be a power of 2!The latest conclusions of senior ML scholars
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
Golang - gin - pprof - use and safety
Ali on three sides: MQ message loss, repetition, backlog problem, how to solve?
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
csdn发文助手问题
随机推荐
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
电脑重要文件很多,如何备份比较安全?
C# 中的Async 和 Await 的用法详解
20.nn.Module
IDEA的database使用教程(使用mysql数据库)
The cluster of safe mode
Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details
Buffer 与 拥塞控制
Text similarity calculation (Chinese and English) detailed explanation of actual combat
Optimization of five data submission methods
SAP e-commerce cloud Spartacus SSR Optimization Engine execution sequence of several timeouts
mysql8, starttime的下一个值作为endtime的上一个值?
Four ways to clear the float and its principle understanding
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
技能大赛训练题:域用户和组织单元的创建
The use of C# control CheckBox
Centos7 install mysql5.7
Spark Learning: Add Custom Optimization Rules for Spark Sql
C#控件ListView用法
C# control ToolStripProgressBar usage