当前位置:网站首页>3种方法解轮转数组
3种方法解轮转数组
2022-07-28 13:19:00 【蓝猫骑士】
题目描述:给你一个数组,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
例如
输入: nums = [1,2,3,4,5,6,7], k = 3
输出: [5,6,7,1,2,3,4]
解释:
向右轮转 1 步: [7,1,2,3,4,5,6]
向右轮转 2 步: [6,7,1,2,3,4,5]
向右轮转 3 步: [5,6,7,1,2,3,4]
我想到的第一种方法就是暴力选择 时间复杂度O(k*N)
当没有明确说明k 的值 根据最坏情况原则 它的时间复杂度 应为 O(N^2)
那么结果毫无悬念 结果又超出了 时间的限制。下面是我的代码 简单易于理解 就不多解释了;
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.使用额外的数组
以额外的空间换取时间;开辟额外的数组 进行存储。
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.反转数组的方法
三次反转
例如
数组 1 2 3 4 5 6 7 k = 3
第一步反转 7 6 5 4 3 2 1
第二步反转 5 6 7 4 3 2 1
第三步反转 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. 我再研究一下
边栏推荐
- 基于NoneBot2的qq机器人配置记录
- Three cases of thread blocking.
- Implementation of StrCmp, strstr, memcpy, memmove
- strcmp、strstr、memcpy、memmove的实现
- Qt5 development from introduction to mastery -- the first overview
- Socket class understanding and learning about TCP character stream programming
- 目标检测:速度和准确性比较(Fater R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)
- Security assurance is based on software life cycle -istio authorization mechanism
- How to configure ADB environment variables (where to open environment variables)
- Operator3 - design an operator
猜你喜欢

什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量

深度学习基础----GNN谱域和空域 (不断完善更新积累)

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

Operator3 - design an operator

Thesis study -- masked generative disintegration

Daily question - Scholarship

MVC模型:日历系统

Qt5 development from introduction to mastery -- the first overview

Algorithm --- different paths (kotlin)

Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
随机推荐
正则表达式
【Utils】ServletUtil
Collaborative office tools: Online whiteboard is in its infancy, and online design has become a red sea
Multi level cache scheme
离散对数问题(DLP) && Diffie-Hellman问题(DHP)
Security assurance is based on software life cycle -istio authorization mechanism
【Utils】JsonUtil
[translation] how to choose a network gateway for your private cloud
安全保障基于软件全生命周期-PSP应用
A label_ File download (download attribute)
strcmp、strstr、memcpy、memmove的实现
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
Security assurance is based on software life cycle - networkpolicy application
【LVGL事件(Events)】事件代码
【Util】redis工具类:把redis的value序列化器修改为GenericJackson2JsonRedisSerializer,就支持返回值为对象或集合了
Literature reading (245) roller
SLAM论文合集
Poj1860 currency exchange solution
7.27模拟赛总结
牛客多校-Link with Level Edito I-(线性dp)