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

边栏推荐
- Read the history of it development in one breath
- 得知女儿被猥亵,35岁男子将对方打至轻伤二级,法院作出不起诉决定
- Simple query cost estimation
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- MySQL之知识点(七)
- ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
- Anaconda中配置PyTorch环境——win10系统(小白包会)
- 力扣解法汇总1200-最小绝对差
- Rider set the highlighted side of the selected word, remove the warning and suggest highlighting
- QT console printout
猜你喜欢

论文阅读_中文NLP_LTP

Mongodb (quick start) (I)

论文阅读_医疗NLP模型_ EMBERT
Database design in multi tenant mode

Why is all (()) true and any (()) false?

Zabbix

mongodb(快速上手)(一)

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

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée
随机推荐
十个顶级自动化和编排工具
mongodb(快速上手)(一)
外盘黄金哪个平台正规安全,怎么辨别?
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
中国银河证券开户安全吗 开户后多久能买股票
Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
Independent development is a way out for programmers
使用QT设计师界面类创建2个界面,通过按键从界面1切换到界面2
Tita performance treasure: how to prepare for the mid year examination?
WebApp开发-Google官方教程
Force deduction solution summary 1200 minimum absolute difference
Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
如何保存训练好的神经网络模型(pytorch版本)
Vulnerability recurrence - 48. Command injection in airflow DAG (cve-2020-11978)
Operation before or after Teamcenter message registration
Short the command line via jar manifest or via a classpath file and rerun
mybash
Cartoon: looking for the k-th element of an unordered array (Revised)
Mongodb (quick start) (I)
Winedt common shortcut key modify shortcut key latex compile button