当前位置:网站首页>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
}
边栏推荐
- 漫画:有趣的海盗问题 (完整版)
- Mysql5.6 parsing JSON strings (supporting complex nested formats)
- CVPR 2022最佳学生论文:单张图像估计物体在3D空间中的位姿估计
- 企业数字化发展中的六个安全陋习,每一个都很危险!
- VBA drives SAP GUI to realize office automation (II): judge whether elements exist
- Clickhouse (03) how to install and deploy Clickhouse
- Why is all (()) true and any (()) false?
- 世界上最难的5种编程语言
- 网络威胁分析师应该具备的十种能力
- Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
猜你喜欢
Which is more cost-effective, haqu K1 or haqu H1? Who is more worth starting with?
Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP
Oracle recovery tools -- Oracle database recovery tool
Seven Devops practices to improve application performance
Kafaka technology lesson 1
mybash
ELK日志分析系统
ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise
论文阅读_中文NLP_LTP
统计php程序运行时间及设置PHP最长运行时间
随机推荐
力扣解法汇总729-我的日程安排表 I
Kafaka technology lesson 1
The five most difficult programming languages in the world
Cartoon: interesting pirate problem (full version)
Redis基础
2022年信息系统管理工程师考试大纲
数据访问 - EntityFramework集成
ITK Example
MATLAB查阅
leetcode每日一练:旋转数组
2022 information system management engineer examination outline
BigDecimal除法的精度问题
中国银河证券开户安全吗 开户后多久能买股票
Server configuration jupyter environment
企业数字化发展中的六个安全陋习,每一个都很危险!
Disorganized series
Ordinary programmers look at the code, and top programmers look at the trend
Count the running time of PHP program and set the maximum running time of PHP
每日一练:关于日期的一系列
Complete solution instance of Oracle shrink table space