当前位置:网站首页>【LeetCode】189-轮转数组
【LeetCode】189-轮转数组
2022-07-02 12:09:00 【酥酥~】
给你一个数组,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。
示例 1:
输入: 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]
示例 2:
输入: nums = [-1,-100,3,99], k = 2
输出: [3,99,-1,-100]
解释:
向右轮转 1 步: [99,-1,-100,3]
向右轮转 2 步: [3,99,-1,-100]
提示:
- 1 <= nums.length <= 105
- -231 <= nums[i] <= 231 - 1
- 0 <= k <= 105
进阶:
尽可能想出更多的解决方案,至少有 三种 不同的方法可以解决这个问题。
你可以使用空间复杂度为 O(1) 的 原地 算法解决这个问题吗?
#利用python数列特性
class Solution(object):
def rotate(self, nums, k):
n = len(nums)
k = k%n
nums[:] = nums[n-k:]+nums[:n-k]
return nums
边栏推荐
- 损失函数与正负样本分配:YOLO系列
- 21_ Redis_ Analysis of redis cache penetration and avalanche
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- Oracle primary key auto increment
- Topology architecture of the minimum deployment of tidb cluster
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- 【LeetCode】486-预测赢家
- 05_队列
- Solve the problem of frequent interruption of mobaxterm remote connection
- 你不知道的Set集合
猜你喜欢
17_ Redis_ Redis publish subscription
06_ Stack and queue conversion
20_ Redis_ Sentinel mode
Leetcode skimming - remove duplicate letters 316 medium
How does the computer set up speakers to play microphone sound
MySQL -- Index Optimization -- order by
04_ Stack
Yolov5 code reproduction and server operation
How to intercept the value of a key from the JSON string returned by wechat?
SQL transaction
随机推荐
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
Build your own semantic segmentation platform deeplabv3+
Beijing rental data analysis
PHP method to get the index value of the array item with the largest key value in the array
党史纪实主题公益数字文创产品正式上线
Practical debugging skills
Leetcode skimming - remove duplicate letters 316 medium
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Steps for Navicat to create a new database
15_ Redis_ Redis. Conf detailed explanation
Force deduction solution summary 2029 stone game IX
彻底弄懂浏览器强缓存和协商缓存
Mavn builds nexus private server
02_ Linear table_ Sequence table
How to write sensor data into computer database
12_Redis_Bitmap_命令
Solve the problem of frequent interruption of mobaxterm remote connection
Force deduction solution summarizes the lucky numbers in 1380 matrix
21_Redis_浅析Redis缓存穿透和雪崩
提前批院校名称