当前位置:网站首页>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
边栏推荐
- 如何有效进行回顾会议(上)?
- [try to hack] hfish honeypot deployment
- 天气这么热太阳能发电不得起飞喽啊?喽啊个头……
- LeetCode 0142.环形链表 II
- LeetCode 105.从前序与中序遍历序列构造二叉树 && 106.从中序与后序遍历序列构造二叉树
- MySQL development skills - View
- How to configure ADB environment variables (where to open environment variables)
- QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
- 第六章 支持向量机
- webSocket聊天
猜你喜欢

草料二维码--在线二维码生成器
![[ecmascript6] set and map](/img/64/dd6ffc5f0faf881b990e609cf62343.png)
[ecmascript6] set and map

MySQL开发技巧——视图

Implementation of StrCmp, strstr, memcpy, memmove

第六章 支持向量机

阿里、京东、抖音:把云推向产业心脏

Redis sentinel mechanism

Chapter 6 support vector machine

83.(cesium之家)cesium示例如何运行

Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
随机推荐
利用反射构建一棵菜单生成树
【LeetCode】1331. 数组序号转换
【LVGL事件(Events)】事件代码
How did Dongguan Huawei cloud data center become a new model of green data center?
Implementation of StrCmp, strstr, memcpy, memmove
Thesis study -- masked generative disintegration
[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)
IP黑白名单
《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
IP black and white list
LeetCode 0143. 重排链表
redis哨兵机制
Three cases of thread blocking.
Security assurance is based on software life cycle - networkpolicy application
【LVGL事件(Events)】事件在不同组件上的应用(一)
文献阅读(245)Roller
Istio IV fault injection and link tracking
RSA用私钥加密数据公钥解密数据(不是签名验证过程)
Clickhouse分布式集群搭建
Clickhouse architecture and design