当前位置:网站首页>[leetcode] 189 rotation array
[leetcode] 189 rotation array
2022-07-02 15:36:00 【Crisp ~】
Give you an array , Rotate the elements in the array to the right k A place , among k It is a non negative number .
Example 1:
Input : nums = [1,2,3,4,5,6,7], k = 3
Output : [5,6,7,1,2,3,4]
explain :
Rotate right 1 Step : [7,1,2,3,4,5,6]
Rotate right 2 Step : [6,7,1,2,3,4,5]
Rotate right 3 Step : [5,6,7,1,2,3,4]
Example 2:
Input : nums = [-1,-100,3,99], k = 2
Output : [3,99,-1,-100]
explain :
Rotate right 1 Step : [99,-1,-100,3]
Rotate right 2 Step : [3,99,-1,-100]
Tips :
- 1 <= nums.length <= 105
- -231 <= nums[i] <= 231 - 1
- 0 <= k <= 105
Advanced :
Come up with as many solutions as you can , There are at least Three Different methods can solve this problem .
You can use a spatial complexity of O(1) Of In situ Does the algorithm solve this problem ?
# utilize python Sequence characteristics
class Solution(object):
def rotate(self, nums, k):
n = len(nums)
k = k%n
nums[:] = nums[n-k:]+nums[:n-k]
return nums
边栏推荐
- Yolo format data set processing (XML to txt)
- 5. Practice: jctree implements the annotation processor at compile time
- Force deduction solution summary 2029 stone game IX
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
- MD5加密
- Leetcode skimming - remove duplicate letters 316 medium
- YOLOV5 代码复现以及搭载服务器运行
- Basic knowledge of cryptography
- 05_ queue
- 你不知道的Set集合
猜你喜欢
5. Practice: jctree implements the annotation processor at compile time
Solve the problem of frequent interruption of mobaxterm remote connection
I made an istio workshop. This is the first introduction
SQL stored procedure
工程师评测 | RK3568开发板上手测试
【网络安全】网络资产收集
你不知道的Set集合
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
. Net again! Happy 20th birthday
Leetcode skimming -- sum of two integers 371 medium
随机推荐
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
How to conduct TPC-C test on tidb
【LeetCode】200-岛屿数量
16_ Redis_ Redis persistence
03.golang初步使用
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
[solution] educational codeforces round 82
搭建自己的语义分割平台deeplabV3+
04. Some thoughts on enterprise application construction after entering cloud native
【LeetCode】1162-地图分析
13_Redis_事务
20_ Redis_ Sentinel mode
How to solve the problem of database content output
夏季高考文化成绩一分一段表
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
21_ Redis_ Analysis of redis cache penetration and avalanche
Yolov5 code reproduction and server operation
NBA player analysis
18_ Redis_ Redis master-slave replication & cluster building
【LeetCode】344-反转字符串