Skip to content

Commit 8a9b472

Browse files
solves rotate array in python
1 parent 3b55b07 commit 8a9b472

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
| 170 | [Two Sum III - Data Structure Design](https://leetcode.com/problems/two-sum-iii-data-structure-design) | Easy | |
5353
| 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ExcelSheetColumnNumber.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/excel_sheet_column_number.py) |
5454
| 172 | [Factoring Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/FactorialTrailingZeros.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/factorial_trailing_zeroes.py) |
55-
| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RotateArray.java) |
55+
| 189 | [Rotate Array](https://leetcode.com/problems/rotate-array) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RotateArray.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/rotate_array.py) |
5656
| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ReverseBits.java) |
5757
| 191 | [Number of One Bits](https://leetcode.com/problems/number-of-1-bits) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NumberOf1Bit.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/number_of_1_bits.py) |
5858
| 198 | [House Robber](https://leetcode.com/problems/house-robber) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/HouseRobber.java) |

Diff for: python/factorial_trailing_zeroes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def trailingZeroes(self, n: int) -> int:
33
divisor, count = 5, 0
4-
while n // divisor > 0:
4+
while divisor <= n:
55
count += n // divisor
66
divisor *= 5
77
return count

Diff for: python/rotate_array.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def rotate(self, nums: List[int], k: int) -> None:
6+
"""
7+
Do not return anything, modify nums in-place instead.
8+
"""
9+
k %= len(nums)
10+
if k == 0:
11+
return
12+
temp = nums[-k:]
13+
for i in range(len(nums) - 1, k - 1, -1):
14+
nums[i] = nums[i - k]
15+
for i, val in enumerate(temp):
16+
nums[i] = val
17+
18+
def reverse(self, A, i, j):
19+
while i < j:
20+
A[i], A[j] = A[j], A[i]
21+
i += 1
22+
j -= 1
23+
24+
# this is method no. 2 to solve it with O(1) space complexity
25+
def rotate_2(self, A, k):
26+
L = len(A)
27+
k %= L
28+
if k:
29+
self.reverse(A, 0, L - 1)
30+
self.reverse(A, 0, k - 1)
31+
self.reverse(A, k, L - 1)

0 commit comments

Comments
 (0)