当前位置:网站首页>【189. 轮转数组】
【189. 轮转数组】
2022-06-11 01:43:00 【爱吃榴莲的喵星人】
一、题目要求
给你一个数组,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
示例 1:
输入: 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]
示例 2:
输入:nums = [-1,-100,3,99], k = 2
输出:[3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]
提示:
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/rotate-array
二、思路分析
思路:
void reverse(int* nums,int left,int right)
{
while(left < right)
{
int tmp= nums[left];
nums[left]=nums[right];
nums[right]=tmp;
left++;
right--;
}
}
void rotate(int* nums, int numsSize, int k){
if(k >= numsSize)
k %= numsSize;
reverse(nums,0,numsSize-k-1);
reverse(nums,numsSize-k,numsSize-1);
reverse(nums,0,numsSize-1);
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- Epoll 原理及应用 && ET模式与LT模式
- SQL | return customer name, relevant order number and total price of each order
- Mentality cannot collapse
- 为什么有的程序员能力一般却能拿到好offer?
- SQL | 计算总和
- Dynamically add attributes to objects
- cannot import name ‘dtensor‘ from ‘tensorflow. compat. v2.experimental‘
- Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
- 如何3步精读《PMBOK指南》(经验+资料分享)
- Navicat Premium 15 工具自动被杀毒防护软件删除解决方法
猜你喜欢

Sd3.0 notes

Navicat Premium 15 工具自动被杀毒防护软件删除解决方法
![[parallel and distributed systems] cache learning](/img/79/de4da45aab54bb3bec240ac36e7978.png)
[parallel and distributed systems] cache learning

mysql重装时写my.ini配置文件出错

【AI周报】AI与冷冻电镜揭示「原子级」NPC结构;清华、商汤提出「SIM」方法兼顾语义对齐与空间分辨能力

Find - (block find)

扁平数据转tree与tree数据扁平化

The diligent is the laziest

Colab报错:ImportError: cannot import name ‘_check_savefig_extra_args‘ from ‘matplotlib.backend_bases‘

Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
随机推荐
可扩/减容线程池C语言原理讲解及代码实现
贵金属白银行情走势图缘何强势?
The diligent is the laziest
C language principle explanation and code implementation of scalable / reduced thread pool
Analysis of common ADB commands
SQL | calculate sum
Defense measures for common vulnerabilities
金属有机骨架材料Fe-MIL-53,Mg-MOF-74,Ti-KUMOF-1,Fe-MIL-100,Fe-MIL-101)负载异氟醚/甲氨蝶呤/阿霉素(DOX)/紫杉醇/布洛芬/喜树碱
Jetpack Compose Scaffold和BottomAppBar(底部导航)
Kotlin apply method
【无标题】
What do you know about the set class? Soul questions from Volume I
【并行与分布式系统】Cache学习
Implementing stack with queue
2022 high altitude installation, maintenance and removal of simulated examination platform of theoretical question bank
2022 simulated 100 questions and answers for crane driver (limited to bridge crane) examination
扁平数据转tree与tree数据扁平化
叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物
Jetpack compose box control
项目 - Redis消息队列+工作线程取出用户操作日志并入库(二)
