当前位置:网站首页>Three methods to disassemble the rotation array
Three methods to disassemble the rotation array
2022-07-28 14:20:00 【Blue cat Knight】
Title Description : Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
for example
Input : nums = [1,2,3,4,5,6,7], k = 3
Output : [5,6,7,1,2,3,4]
explain :
Rotate right 1 Step : [7,1,2,3,4,5,6]
Rotate right 2 Step : [6,7,1,2,3,4,5]
Rotate right 3 Step : [5,6,7,1,2,3,4]
The first method I think of is violent choice Time complexity O(k*N)
When there is no clear statement k Value According to the worst-case principle Its time complexity Should be O(N^2)
Then there is no suspense about the result The result is beyond Time limit . Here is my code Easy to understand There's not much to explain ;
void rotate(int* nums, int numsSize, int k){
int i = 0;
int j = 0;
for (i = 0; i < k; i++)
{
int tmp = nums[numsSize - 1];
for (j = numsSize-1; j > 0; j--)
{
nums[j] = nums[j - 1];
}
nums[0] = tmp;
}
return nums;
}1. Use extra arrays
Exchange extra space for time ; Open up additional arrays For storage .
void rotate(int* nums, int numsSize, int k){
int arr[100000] = {0};
int i = 0;
k %= numsSize;
for(i = 0;i<k;i++)
{
arr[i] = nums[numsSize-k+i];
}
for(i = k;i<numsSize;i++)
{
arr[i] = nums[i-k];
}
for(i = 0;i<numsSize;i++)
{
nums[i] = arr[i];
}
return nums;
}2. How to invert an array
Three reverses
for example
Array 1 2 3 4 5 6 7 k = 3
The first step is to reverse 7 6 5 4 3 2 1
Step 2 reverse 5 6 7 4 3 2 1
Step 3 reverse 5 6 7 4 1 2 3
void swap(int* a, int* b) {
int t = *a;
*a = *b, *b = t;
}
void reverse(int* nums, int start, int end) {
while (start < end) {
swap(&nums[start], &nums[end]);
start += 1;
end -= 1;
}
}
void rotate(int* nums, int numsSize, int k) {
k %= numsSize;
reverse(nums, 0, numsSize - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, numsSize - 1);
}
3. I'll study it again
边栏推荐
- Install mysql5.7.36 in CentOS
- Super resolution reconstruction based on deep learning
- Thesis study -- masked generative disintegration
- Several solutions to spanning
- MySQL开发技巧——视图
- Socket class understanding and learning about TCP character stream programming
- 文献阅读(245)Roller
- Multi level cache scheme
- redis哨兵机制
- [ecmascript6] set and map
猜你喜欢

JMeter installation tutorial and login add token

Thesis study -- masked generative disintegration

Security assurance is based on software life cycle -istio authentication mechanism

Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

Read how to deploy highly available k3s with external database

【LVGL事件(Events)】事件在不同组件上的应用(一)

Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记

Clickhouse架构与设计

Leetcode 0142. circular linked list II
随机推荐
利用反射构建一棵菜单生成树
[try to hack] hfish honeypot deployment
QML picture preview
Entering the world of audio and video -- flv video packaging format
How did Dongguan Huawei cloud data center become a new model of green data center?
How to effectively conduct the review meeting (Part 1)?
Docker deploys Mysql to realize remote connection [easy to understand]
Multithreading and high concurrency (III) -- source code analysis AQS principle
LeetCode 1331.数组序号转换
第六章 支持向量机
redis哨兵机制
记一次COOKIE的伪造登录
Daily question - Scholarship
Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
【Utils】JsonUtil
Several solutions to spanning
HCIP第十一天
[ecmascript6] set and map
Thesis study -- masked generative disintegration
The default storage engine after MySQL 5.5 is InnoDB.