当前位置:网站首页>【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
边栏推荐
- 21_Redis_浅析Redis缓存穿透和雪崩
- 02.面向容器化后,必须面对golang
- 12_Redis_Bitmap_命令
- 语义分割学习笔记(一)
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
- 04. Some thoughts on enterprise application construction after entering cloud native
- How to intercept the value of a key from the JSON string returned by wechat?
- 高考录取分数线爬虫
- Markdown tutorial
猜你喜欢

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测

YOLOV5 代码复现以及搭载服务器运行

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

让您的HMI更具优势,FET-G2LD-C核心板是个好选择

17_Redis_Redis发布订阅

Case introduction and problem analysis of microservice

做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
随机推荐
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
04. Some thoughts on enterprise application construction after entering cloud native
20_ Redis_ Sentinel mode
Mavn builds nexus private server
02. After containerization, you must face golang
Build your own semantic segmentation platform deeplabv3+
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
02.面向容器化后,必须面对golang
Evaluation of embedded rz/g2l processor core board and development board of Feiling
06_栈和队列转换
Map introduction
11_Redis_Hyperloglog_命令
15_ Redis_ Redis. Conf detailed explanation
LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
21_Redis_浅析Redis缓存穿透和雪崩
05_ queue
【网络安全】网络资产收集
Force deduction solution summarizes the lucky numbers in 1380 matrix
Real estate market trend outlook in 2022
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568