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

边栏推荐
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
- ITK Example
- Read libco save and restore the on-site assembly code
- Zabbix
- Cartoon: interesting pirate problem (full version)
- Which platform of outer disk gold is regular and safe, and how to distinguish it?
- flask接口响应中的中文乱码(unicode)处理
- Sentinel-流量防卫兵
- 企业数字化发展中的六个安全陋习,每一个都很危险!
- The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
猜你喜欢

ICML 2022 | Meta propose une méthode robuste d'optimisation bayésienne Multi - objectifs pour faire face efficacement au bruit d'entrée

Oracle recovery tools -- Oracle database recovery tool

查看自己电脑连接过的WiFi密码

解决“双击pdf文件,弹出”请安装evernote程序

Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!

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

Cmake tutorial Step2 (add Library)

Winedt common shortcut key modify shortcut key latex compile button

漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)

Mongodb (quick start) (I)
随机推荐
Oracle recovery tools -- Oracle database recovery tool
Cartoon: how to multiply large integers? (I) revised version
Cartoon: looking for the k-th element of an unordered array (Revised)
Short the command line via jar manifest or via a classpath file and rerun
Complete solution instance of Oracle shrink table space
哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?
leetcode每日一题:字符串中的第一个唯一字符
漫画:一道数学题引发的血案
Count the running time of PHP program and set the maximum running time of PHP
Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions
论文阅读_医疗NLP模型_ EMBERT
服务器配置 jupyter环境
Six bad safety habits in the development of enterprise digitalization, each of which is very dangerous!
統計php程序運行時間及設置PHP最長運行時間
Ten capabilities that cyber threat analysts should have
提高應用程序性能的7個DevOps實踐
2022 information system management engineer examination outline
Ordinary programmers look at the code, and top programmers look at the trend
EPM related
7 pratiques devops pour améliorer la performance des applications