当前位置:网站首页>189. 轮转数组
189. 轮转数组
2022-06-11 21:36:00 【爱学代码的学生】
题目描述:
给你一个数组,将数组中的元素向右轮转 k个位置,其中 k是非负数。
题目分析:
1. 移动数组
我们存储数组中的最后一个元素,然后移动整个数组,最后把存储的元素放在第一个位置,重复k次。
我们这里设数组是num[7]={1,2,3,4,5,7,}
k=3
代码如下:
2. 开辟额外的数组
我们可以提前将元素放在移动后的位置,最后赋值给原数组
代码如下:
3. 数组旋转
操作 结果 1 2 3 4 5 6 7 1 2 3 4 5 6 7 翻转[0,numsSize-1-k] 4 3 2 1 5 6 7 翻转[numsSize-k,numsSize-1] 4 3 2 1 7 6 5 翻转整个数组 5 6 7 1 2 3 4 代码实现如下:
边栏推荐
- 线性表的链式存储结构
- Hangzhou Electric Zhongchao 91006 guess the weight
- Analysis on the development history and market development status of China's system integration industry in 2020 [figure]
- Some error reporting assemblies of cann code
- 数据库每日一题---第9天:销售员
- [v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
- Answer fans' questions | count the number and frequency of letters in the text
- Game client performance (memory) [previous]
- servlet获取表单数据
- Leetcode-110-balanced binary tree
猜你喜欢

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

Flutter series: detailed explanation of container layout commonly used in flutter

Master of a famous school has been working hard for 5 years. AI has no paper. How can the tutor free range?

LeetCode-104-二叉树的最大深度

LabVIEW控制Arduino实现超声波测距(进阶篇—5)

Answer fans' questions | count the number and frequency of letters in the text

LeetCode-76-最小覆盖子串

EndnoteX9簡介及基本教程使用說明

Leetcode-129- sum of numbers from root node to leaf node

Leetcode-322- change exchange
随机推荐
Redis transaction
2021-09-11 训练场补题
Redis Foundation
SQL的语法
A problem of setting the private library of golang
RPA丨首席财务官如何找到数字化转型“超级入口”?
Codeforces Round #742 (Div. 2) F. One-Four Overload
As a senior abap consultant, which SAP technology can be selected as the main direction in the next step?
行而不辍,未来可期|云扩科技入选上海市专精特新企业
js对返回的数据的各种数据类型进行非空判断。
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
Leetcode-322- change exchange
Redis basic data type (list)
Deploy SAP ui5 applications to the sap BTP kyma operating environment step by step
LaTex实战笔记 3-宏包与控制命令
RPA超自动化 | 农耕记携手云扩加速财务智能化运营
JS performs non empty judgment on various data types of the returned data.
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
LeetCode-32-最长有效括号
How to use RPA robot to start the first step of digital transformation of freight forwarding industry?


