forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189_Rotate_Array
42 lines (37 loc) · 995 Bytes
/
189_Rotate_Array
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
// https://leetcode.com/problems/rotate-array/
// MY SOLUTION
class Solution {
public void rotate(int[] nums, int k) {
int len = nums.length;
if(k==0){
for(int i=0;i<len;i++)
System.out.println(nums[i]);
}
else
{
int x = nums[len-1];
int temp = nums[0];
for(int i=1;i<(len);i++){
int m = nums[i];
nums[i] = temp;
temp = m;
}
nums[0] = x;
rotate(nums,k-1);
}
}
}
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k%len(nums)
num = nums[::-1]
a = num[:k][::-1]
b = num[k:][::-1]
x = a + b
for i in range(len(nums)):
nums[i] = x[i]
return nums
// https://leetcode.com/problems/rotate-array/solution/