-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotateArray.py
More file actions
67 lines (63 loc) · 1.57 KB
/
rotateArray.py
File metadata and controls
67 lines (63 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'''
Source : https://leetcode.com/problems/rotate-array/
Author : Yuan Wang
Date : 2018-06-04
/**********************************************************************************
*
* Rotate an array of n elements to the right by k steps.
* For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
*
*Example 1:
*
* Input: [-1,-100,3,99] and k = 2
* Output: [3,99,-1,-100]
* Explanation:
* rotate 1 steps to the right: [99,-1,-100,3]
* rotate 2 steps to the right: [3,99,-1,-100]
* Note:
* Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
*
* Hint:
* Could you do it in-place with O(1) extra space?
*
* Related problem: Reverse Words in a String II
*
*
*
**********************************************************************************/
'''
def rotateA(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
nums[:] = nums[n-k:] + nums[:n-k]
def rotateB(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
if len(nums) != k:
for i in range(k):
element=nums.pop()
nums.insert(0,element)
def rotateC(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: void Do not return anything, modify nums in-place instead.
"""
temp=[]
for i in range(len(nums)):
temp.append(0)
for i in range(len(nums)):
temp[(i+k) % len(nums)]=nums[i]
for i in range(len(nums)):
nums[i]=temp[i]
nums=[1,2]
k=10
rotateA(nums,3)
print(nums)