当前位置:网站首页>【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
边栏推荐
- Infra11199 database system
- 彻底弄懂浏览器强缓存和协商缓存
- Tidb hybrid deployment topology
- Redux——详解
- 06_栈和队列转换
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
- Tidb data migration tool overview
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- 夏季高考文化成绩一分一段表
猜你喜欢

03. Preliminary use of golang

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

MySQL -- Index Optimization -- order by

. Solution to the problem of Chinese garbled code when net core reads files

17_Redis_Redis发布订阅

Bing.com網站

Practical debugging skills

03_ Linear table_ Linked list

Map introduction

06_ Stack and queue conversion
随机推荐
16_ Redis_ Redis persistence
21_Redis_浅析Redis缓存穿透和雪崩
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
15_Redis_Redis.conf详解
07_ Hash
自定义异常
Leetcode skimming -- sum of two integers 371 medium
13_Redis_事务
Learn the method code example of converting timestamp to uppercase date using PHP
党史纪实主题公益数字文创产品正式上线
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Custom exception
Build your own semantic segmentation platform deeplabv3+
搭建自己的语义分割平台deeplabV3+
Common English abbreviations for data analysis (I)
LeetCode刷题——奇偶链表#328#Medium
Mavn builds nexus private server
SQL transaction
Infra11199 database system
20_ Redis_ Sentinel mode