当前位置:网站首页>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];
}
}边栏推荐
- 技能大赛dhcp服务训练题
- 技能大赛训练题:MS15_034漏洞验证与安全加固
- 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
- 3.爬虫之Scrapy框架1安装与使用
- IDEA can't find the Database solution
- Text similarity calculation (Chinese and English) detailed explanation of actual combat
- 六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
- 技能大赛训练题:登录安全加固
- 生产力工具和插件
- 如何使用StarUML画类图[通俗易懂]
猜你喜欢

IDEA can't find the Database solution

技能大赛训练题:MS15_034漏洞验证与安全加固

ECCV 2022 | Robotic Interaction Perception and Object Manipulation

C#控件ListView用法

Solution for browser hijacking by hao360

报错IDEA Terminated with exit code 1

networkx绘制度分布

抓住金三银四的尾巴,解锁程序员面试《刷题神器》

C#使用NumericUpDown控件

How to quickly split and merge cell data in Excel
随机推荐
PartImageNet物体部件分割(Semantic Part Segmentation)数据集介绍
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
Introduction to using NPM
Sliding window method to segment data
ECCV 2022 | Robotic Interaction Perception and Object Manipulation
IDEA can't find the Database solution
分布式锁有哪些,怎么实现(分布式锁的三种实现的对比)
PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
求一份常见Oracle故障模拟场景
MATLAB | 我也做了一套绘图配色可视化模板
图像大面积缺失,也能逼真修复,新模型CM-GAN兼顾全局结构和纹理细节
操作符详解
基于去噪自编码器的故障隔离与识别方法
PHP序列化:eval
golang-gin-优雅重启
Error IDEA Terminated with exit code 1
The cluster of safe mode
Selenium自动化测试之Selenium IDE
技能大赛训练题:交换机的远程管理