当前位置:网站首页>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
}

边栏推荐
- 中国银河证券开户安全吗 开户后多久能买股票
- Interpretation: how to deal with the current security problems faced by the Internet of things?
- CVPR 2022 best student paper: single image estimation object pose estimation in 3D space
- Cartoon: a bloody case caused by a math problem
- ELK日志分析系统
- “12306” 的架构到底有多牛逼?
- This 17-year-old hacker genius cracked the first generation iPhone!
- Data access - entityframework integration
- Matlab reference
- Winedt common shortcut key modify shortcut key latex compile button
猜你喜欢

北京内推 | 微软亚洲研究院机器学习组招聘NLP/语音合成等方向全职研究员

GFS分布式文件系统

网络威胁分析师应该具备的十种能力

C # mixed graphics and text, written to the database in binary mode

ELK日志分析系统

mongodb(快速上手)(一)

Cmake tutorial Step2 (add Library)

提高应用程序性能的7个DevOps实践

CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
随机推荐
解决“双击pdf文件,弹出”请安装evernote程序
每日一练:关于日期的一系列
企业数字化发展中的六个安全陋习,每一个都很危险!
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
证券网上开户安全吗?证券融资利率一般是多少?
Is it safe to open an account online? What is the general interest rate of securities financing?
CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
QT console printout
mongodb(快速上手)(一)
Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
QT控制台打印输出
Cmake tutorial step6 (add custom commands and generate files)
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
Force deduction solution summary 1200 minimum absolute difference
普通程序员看代码,顶级程序员看趋势
论文阅读_医疗NLP模型_ EMBERT
Cmake tutorial Step4 (installation and testing)
云主机oracle异常恢复----惜分飞