Leetcode - Rotate Array (Python)

preview_player
Показать описание
October 2020 Leetcode Challenge
Leetcode - Rotate Array # 189
Рекомендации по теме
Комментарии
Автор

using dequeus
from collections import deque
d=deque(l)
d.rotate(k)
print(d)
for left, its -k and for right, its K

MrNarendra
Автор

I did not know you could delete a slice! Thanks Tim!

janmichaelaustria
Автор

Hello, can you explain the Line Number 8 at 2:30

temp = [nums[i] for i in range(len(nums))]

I never actually understood the for loop in the second solution

for i in range(len(nums)):
nums[(i + k) % len(nums)] = temp[i].

Can you please explain this part for me. Would be greatly appreciated

kiranmenon
Автор

You don’t need to cut k twice, based on your solution:

class Solution:
def rotate(self, nums: List[int], k: int) -> None:
tmp = list(nums)
for i in range(len(nums)): nums[(i+k)%len(nums)] = tmp[i]

= Accepted by leetcode

jonaspertschy
Автор

l=[1, 2, 3, 4, 5, 6, 7]
k=3
print(l[-k:]+l[:(k+1)])

MrNarendra
Автор

Thank you very much for your video sir. Following is my solution based on your concept only.
n=[1, 2, 3, 4, 5]
k=2
l=len(n)
x=k%l
n[-x:], n[:0]=n[:0], n[-x:]
print (n)

sharifmansuri