当前位置:网站首页>Leetcode daily practice - 189. Rotation array
Leetcode daily practice - 189. Rotation array
2022-07-26 19:48:00 【An airliner flying to the stars】
Preface
Wassup guys! I am a Edison
It's today LeetCode Upper leetcode 189. Rotation array
Let’s get it!

List of articles
1. Topic analysis
Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
Example 1:
Example 2:
2. Title diagram
Train of thought : Right hand k Time , Move one... In turn
Suppose we want to put the array [1,2,3,4,5,6,7], Spin to the right 3 Time 
The first 1 Step , Define a variable tmp Used to store the last element of the array 7;
The first 2 Step , Before array n-1 Move the value back ;
The first 3 Step , hold tmp The value of is placed in the first empty position ;
This completes a right rotation , So we are rotating to the right k Time , You get the final result .
The time complexity of this method is O ( N ∗ K ) O(N*K) O(N∗K); The space complexity is O ( 1 ) O(1) O(1);
When K % N be equal to N-1 when , The worst , Because the time complexity is O ( N 2 ) O(N^2) O(N2);
Train of thought two : Extra open array
This method is based on Space for time How to do it .
It's simple , Open up another array , Put it behind k Put elements into the new array front , Put the front n-k Put a new array Back .(n Is the number of elements in the array ).
The time complexity of this method is O ( N ) O(N) O(N), The space complexity is O ( N ) O(N) O(N)
Train of thought three : Three times reverse
This method is very most !, We can go through Three times reverse To solve , Or the following array 
The first 1 Trip to , To the front of the array n - k An inverse , As shown in the figure 
The first 2 Trip to , After the array k An inverse , As shown in the figure 
The first 3 Trip to , And then put the array Global inversion , As shown in the figure 
The time complexity of this method is O ( N ) O(N) O(N), The space complexity is O ( 1 ) O(1) O(1).
3. Algorithm design
We could write one Inversion function reverse To realize the main part
Code implementation
void reverse(int* nums, int left, int right) {
while (left < right) {
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
++left;
--right;
}
}
4. Code implementation
We use inverse functions reverse Pay attention when transferring parameters : The subscript of an array is from 0 At the beginning , such as : front 10 The element is 0 To 9;
Because the first 1 Go ahead n - k Inversion , here n = 7,k = 3, Then it's about the front 4 An inverse , But the subscript of the array is from 0 At the beginning , That is to say, before 4 The subscript of an element is 0 To 3, As shown in the figure 
The first 2 Trip to , After the array k An inverse , That is after 3 Elements are inverted , That is, from the subscript 4 To 6 Start reverse , As shown in the figure 
The first 3 Trip to , Invert the entire array , As shown in the figure 
But we should also pay attention to one problem , If k Exceeds the number of array elements , What shall I do? ?
such as : An array is [ 1 2 3 4 5 ] ,k = 6 When , At this time, the number of array elements is 5, It requires right rotation 6 A place , If according to the above analysis , Before the first trip n - k Inversion , That is to say 5 - 6 An inverse , Is it right -1 An inverse position ?
Let's first look at rotating the array to the right 6 How about a position , As shown in the figure 
Why ! You will find that the final result is Rotate right 1 A place Did you? ?
At this point, we should remember that the core of this problem is Rotation array , That is, when the number of rotations exceeds the length of the array , It's a new round !
So we can start with k Remainder , And then rotate again ; For example, the array length is 8, I want to rotate to the right 10 Time , In fact, it turns to the right 2 Time , that 10 % 2 The result is 2 Do you ?
therefore , If k Greater than array length , So first of all k Remainder , And then rotate again , This will not affect the final result !
Interface code
void reverse(int* nums, int left, int right) {
while (left < right) {
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
++left;
--right;
}
}
void rotate(int* nums, int numsSize, int k) {
k %= numsSize; // If k Greater than array length , First pair k Remainder
reverse(nums, 0, numsSize - k - 1);
reverse(nums, numsSize - k, numsSize - 1);
reverse(nums, 0, numsSize - 1);
}
Submit results 
边栏推荐
- How to write the test case of mobile app? What are the mobile app test points?
- conda转移项目虚拟环境必备技能+pip速度下载太慢解决办法
- Linear algebra Chapter 3 vector
- DDL,DQL,DML语句
- What is federated graph machine learning? A summary of the latest "federal map machine learning: concepts, techniques, and Applications" at the University of Virginia
- 带你熟悉云网络的“电话簿”:DNS
- Leetcode-138-copy linked list with random pointer
- Redis introduction
- 中天钢铁在 GPS、 AIS 调度中使用 TDengine
- YOLO V1详解
猜你喜欢

Principle analysis and source code interpretation of service discovery

安全测试与功能测试、渗透测试你知道有什么区别吗?

How to compress the traffic consumption of APP under mobile network in IM development

还在用Xshell?你out了,推荐一个更现代的终端连接工具

Software process that testers must know

数据库设计三大范式

基于华为云 IOT 设计智能称重系统 (STM32)【一】

Intensive reading of the paper: yolov2 - yolo9000: better, faster, stronger

IJCAI2022开会了! Brescia等《证据推理和学习》教程,阐述其最新进展,附96页Slides

聊聊如何用 Redis 实现分布式锁?
随机推荐
win11 edge怎么卸载?win11 edge浏览器彻底卸载的方法教程
3r平衡型理财产品会有风险吗?风险大吗?
Weekend highlights review | establishment of digital RMB industry alliance; China Mobile announced that hefeixin will stop its service
ipad下载的文件在哪里可以找到
Selenium+Web自动化框架的Case
[PHP] save session data to redis
【PHP】MySQL原生PHP操作-天龙八步
【实习经验】异常处理与访问url结果响应数据处理
LeetCode每日一练 —— 88. 合并两个有序数组
What is a knowledge management system? You need to know this
CONDA transfer project virtual environment essential skills +pip speed download too slow solution
Redis介绍
博客维护记录之图片预览嵌入位置问题
Detailed explanation of Yolo v1
What is federated graph machine learning? A summary of the latest "federal map machine learning: concepts, techniques, and Applications" at the University of Virginia
Reentrantlock learning - Basic Attributes
Familiarize you with the "phone book" of cloud network: DNS
基于华为云 IOT 设计智能称重系统 (STM32)【二】结尾有资料
2022/07/26 学习笔记 (day16) 抽象与接口
Implementing DDD based on ABP -- domain logic and application logic