当前位置:网站首页>LeetCode每日一练 —— 189. 轮转数组
LeetCode每日一练 —— 189. 轮转数组
2022-07-26 18:33:00 【飞向星的客机】
前言
Wassup guys!我是Edison
今天是 LeetCode 上的 leetcode 189. 轮转数组
Let’s get it!

1. 题目分析
给你一个数组,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
示例 1:
示例 2:
2. 题目图解
思路一:右旋 k 次,依次移动一个
假设我们要把数组 [1,2,3,4,5,6,7],向右旋转 3 次
第 1 步,定义一个变量 tmp 用于存放数组的最后一个元素 7;
第 2 步,把数组前 n-1 个值往后挪;
第 3 步,把 tmp 的值放入空出来的第一个位置中;
这就完成了一次右旋,那么我们在向右旋转 k 次,就得到了最后的结果。
此方法时间复杂度为 O ( N ∗ K ) O(N*K) O(N∗K);空间复杂度为 O ( 1 ) O(1) O(1);
当 K % N 等于 N-1 时,最坏,因为此时时间复杂度为 O ( N 2 ) O(N^2) O(N2);
思路二:额外开数组
这种方法是以 空间换时间 的做法。
很简单,再开辟一个数组,把后 k 个元素放到新数组 前面,再把前 n-k个放到新数组 后面。(n 为数组中的元素个数)。
此方法时间复杂度为 O ( N ) O(N) O(N),空间复杂度为 O ( N ) O(N) O(N)
思路三:三趟逆置
这种方法非常的 绝!,我们可以通过 三趟逆置 来解决,还是下面这个数组
第 1 趟,对数组的前 n - k 个逆置,如图所示
第 2 趟,对数组的后 k 个逆置,如图所示
第 3 趟,再把数组 整体逆置,如图所示
此方法时间复杂度为 O ( N ) O(N) O(N),空间复杂度为 O ( 1 ) O(1) O(1)。
3. 算法设计
我们可以写一个 逆置 函数 reverse 来实现主要部分
代码实现
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. 代码实现
我们使用逆置函数 reverse 进行传参的时候要注意:数组的下标是从 0 开始的,比如:前 10 个元素是 0 到 9;
因为第 1 趟要对前 n - k 逆置,此时 n = 7,k = 3,那么就是要对前 4 个逆置,但是数组的下标是从 0 开始的,也就是说前 4 个元素的下标是 0 到 3,如图所示
第 2 趟,对数组的后 k 个逆置,也就是后 3 个元素逆置,也就是从下标 4 到 6 开始逆置,如图所示
第 3 趟,把整个数组逆置,如图所示
但是我们还要注意一个问题,如果 k 超过了数组元素的个数,怎么办呢?
比如:数组是 [ 1 2 3 4 5 ] ,k = 6 的时候,此时数组元素个数为 5,而要求向右旋转 6 个位置,如果按照上面分析的情况,第一趟对前 n - k 逆置,也就是 5 - 6 个逆置,难道对前 -1 个逆置吗?
我们先来看一下把数组向右轮转 6 个位置会怎么样,如图所示
咦!你会发现最后结果不就是 向右轮转 1 个位置 了吗?
此时我们要记住这道题的核心叫 轮转数组,也就是当轮转的次数超过数组长度的时候,又是新的一轮了!
所以我们可以先对 k 取余,然后再次旋转;比如数组长度为 8,我要向右轮转 10 次,其实就是向右轮转了 2 次,那么 10 % 2 的结果补就是 2 吗?
所以,如果 k 大于数组长度,故首先对 k 取余,然后再次旋转,这不会影响最终结果!
接口代码
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; // 如果k大于数组长度,先对k取余
reverse(nums, 0, numsSize - k - 1);
reverse(nums, numsSize - k, numsSize - 1);
reverse(nums, 0, numsSize - 1);
}
提交结果
边栏推荐
- Redis6
- 什么是服务器集群?海外服务器集群的优势?
- 服务发现原理分析与源码解读
- Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
- C # get local time / system time
- C language - Introduction - syntax - string (11)
- 如何保护电子商务网站免受网络攻击?
- Turn off win10 automatic update completely
- 洋葱集团携手OceanBase实现分布式升级,全球数据首次实现跨云融合
- The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown
猜你喜欢

论文精读:YOLOV2——YOLO9000:Better, Faster, Stronger

【C语言实现】----动态/文件/静态通讯录

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

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

Redis introduction

C language - Introduction - syntax - string (11)
![[C language implementation] - dynamic / file / static address book](/img/5a/655d9a4799b3e874a454a183dee3f1.png)
[C language implementation] - dynamic / file / static address book

After working for 13 years, I have a little software testing experience and feelings

To add a composite primary key

YOLO V2详解
随机推荐
JS中的 作用域
Introduce the difference between @getmapping and @postmapping in detail
Configure the server environment
Spatiotemporal prediction 5-gat
C language - Introduction - syntax - string (11)
2022 build enterprise level data governance system
The authentication type 10 is not supported
测试人员必须知道的软件流程
周末看点回顾|数字人民币产业联盟成立;中国移动宣布:和飞信将停止服务…
Don't casually pass the request to the asynchronous thread. You can't handle it. You have to use the startasync method
Onion group joined hands with oceanbase to realize distributed upgrading, and global data has achieved cross cloud integration for the first time
“蔚来杯“2022牛客暑期多校训练营2
Selenium + case of Web Automation Framework
The inventory of chips in the United States is high, and the shipment of chips in China has increased rapidly and the import of 28.3 billion chips has been greatly reduced. TSMC has a showdown
Pads draw 2.54mm row needle
Wechat applet plug-in -- wxml to canvas (generate pictures)
如果密钥忘记,多个设备分别不同的密钥,云端是如何同步
DOM案例:10秒倒计时-写跳转页面相关的知识
If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
[skim] two point answer solution