当前位置:网站首页>[189. rotation array]
[189. rotation array]
2022-06-11 02:36:00 【Cat star people who love Durian】
List of articles
One 、 Subject requirements
Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
Example 1:
Input : nums = [1,2,3,4,5,6,7], k = 3
Output : [5,6,7,1,2,3,4]
explain :
Rotate right 1 Step : [7,1,2,3,4,5,6]
Rotate right 2 Step : [6,7,1,2,3,4,5]
Rotate right 3 Step : [5,6,7,1,2,3,4]
Example 2:
Input :nums = [-1,-100,3,99], k = 2
Output :[3,99,-1,-100]
explain :
Rotate right 1 Step : [99,-1,-100,3]
Rotate right 2 Step : [3,99,-1,-100]
Tips :
1 <= nums.length <= 105
-231 <= nums[i] <= 231 - 1
0 <= k <= 105
source : Power button (LeetCode)
link :https://leetcode.cn/problems/rotate-array
Two 、 Thought analysis
Ideas :
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);
}
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
猜你喜欢

What is the relationship between precious metal silver and spot Silver

金属有机框架MOF-Al(DIBA),MOF-Zr(DIBA),MOF-Fe(DIBA)包载姜黄素/羧苄西林/MTX甲氨蝶呤/紫杉醇PTX/阿霉素DOX/顺铂CDDP/CPT喜树碱等药物

clang-format 最全格式说明

SQL | calculate sum

Colab reported an error: importerror: cannot import name '_ check_ savefig_ extra_ args‘ from ‘matplotlib. backend_ bases‘

ADVANCE.AI首席执行官寿栋将在2022新兴市场品牌出海线上峰会分享跨境电商运用AI技术合规

SQL | 计算总和

Write my Ini configuration file error

SD3.0笔记

Learning Tai Chi Maker - esp8226 (II)
随机推荐
Kotlin let method
企业展厅设计能为企业带来什么?
逃离大城市的年轻人:扛住了房价和压力,没扛住流行病
Koa2 learning notes
扁平数据转tree与tree数据扁平化
Jetpack Compose Scaffold和TopAppBar(顶部导航)
How to read PMBOK guide in 3 steps (experience + data sharing)
SQL | external connection
Jetpack Compose Box控件
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
当逻辑删除遇上唯一索引,遇到的问题和解决方案?
2022 safety officer-b certificate examination question bank and answers
error exepected identifier before ‘(‘ token, grpc 枚举类编译错误
Explanation of spark common parameters
多级介孔有机金属骨架材料ZIF-8负载乳酸氧化酶(LOD)/四氧化三铁(Fe304)/阿霉素DOX/胰岛素/cas9蛋白/甲硝唑/大黄素甲醚
金属有机框架MOF-Al(DIBA),MOF-Zr(DIBA),MOF-Fe(DIBA)包载姜黄素/羧苄西林/MTX甲氨蝶呤/紫杉醇PTX/阿霉素DOX/顺铂CDDP/CPT喜树碱等药物
Introduction for i-Teams
2022 high altitude installation, maintenance and removal of simulated examination platform of theoretical question bank
【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区
[untitled]
