当前位置:网站首页>Leetcode daily practice: rotating arrays
Leetcode daily practice: rotating arrays
2022-07-05 17:48:00 【Sharp blade CC】
link : Rotated array
Ask us to transfer , Is to move the last number to the front , Then keep the other numbers in order and move one step back .
Conventional thinking : Just use while loop , The title requires us to rotate n Time , We'll be back n Move the numbers to the front . Although this idea works , But think about it , Move the front number back each time , This time complexity is not low , So we won't talk about this conventional idea here .
New ideas : The title requires us to rotate the array , In fact, we can First flip the entire array in order , then Before we move on n - 1 Number reversal order , Finally, after numsSize - n Number reversal order ( Suppose the number of arrays here is numsSize individual ), In fact, the array rotation is completed .
notes : Here's a key point , Namely If you flip n Time , and n Equal to the length of the array numsSize Words , In fact, the original array does not move , And so on , So we can n Do something about it .
And the spatial complexity of this idea is O(1) Of !
Code :
void partRotate(int* a, int left, int right)
{
// Use left and right pointers
while(left < right)
{
int tmp = a[left];
a[left] = a[right];
a[right] = tmp;
left++;
right--;
}
}
void rotate(int* nums, int numsSize, int k){
k %= numsSize;
// if k The finished length of the die is 0, Then there is no need to rotate
if(k == 0)
return;
partRotate(nums, 0, numsSize - 1);// First flip the entire array
partRotate(nums, 0, k - 1);// Then flip the front k - 1 individual
partRotate(nums, k, numsSize - 1);// Finally, after turning numsSize - k individual
}
边栏推荐
- Is it safe to open an account online? What is the general interest rate of securities financing?
- 服务器配置 jupyter环境
- LeetCode 练习——206. 反转链表
- Oracle Recovery Tools ----oracle数据库恢复利器
- 统计php程序运行时间及设置PHP最长运行时间
- 删除数组中的某几个元素
- Independent development is a way out for programmers
- Teamcenter 消息注册前操作或后操作
- 漫画:有趣的【海盗】问题
- Which platform of outer disk gold is regular and safe, and how to distinguish it?
猜你喜欢
求解为啥all(())是True, 而any(())是FALSE?
Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
IDEA 项目启动报错 Shorten the command line via JAR manifest or via a classpath file and rerun.
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
提高应用程序性能的7个DevOps实践
Redis+caffeine two-level cache enables smooth access speed
Database design in multi tenant mode
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
Winedt common shortcut key modify shortcut key latex compile button
随机推荐
忽米沄析:工业互联网标识解析与企业信息系统的融合应用
leetcode每日一题:字符串中的第一个唯一字符
统计php程序运行时间及设置PHP最长运行时间
漫画:有趣的海盗问题 (完整版)
蚂蚁金服的暴富还未开始,Zoom的神话却仍在继续!
Tita performance treasure: how to prepare for the mid year examination?
Thesis reading_ Medical NLP model_ EMBERT
Is it safe for China Galaxy Securities to open an account? How long can I buy stocks after opening an account
得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
Anaconda中配置PyTorch环境——win10系统(小白包会)
C # realizes crystal report binding data and printing 3-qr code barcode
力扣解法汇总729-我的日程安排表 I
7 pratiques devops pour améliorer la performance des applications
Webapp development - Google official tutorial
[binary tree] insufficient nodes on the root to leaf path
Cartoon: how to multiply large integers? (I) revised version
Complete solution instance of Oracle shrink table space
「运维有小邓」用于云应用程序的单点登录解决方案
Winedt common shortcut key modify shortcut key latex compile button
十个顶级自动化和编排工具