post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3
values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/rotate-array/discuss/1419527/Python-or-Two-Pointers-solution | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
def twopt(arr, i, j):
while (i < j):
arr[i], arr[j] = arr[j], arr[i]
i += 1
j -= 1... | rotate-array | Python | Two-Pointers solution | Shreya19595 | 31 | 3,900 | rotate array | 189 | 0.392 | Medium | 3,000 |
https://leetcode.com/problems/rotate-array/discuss/1168393/One-liner-in-Python3 | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | One liner in Python3 | user3912v | 16 | 1,000 | rotate array | 189 | 0.392 | Medium | 3,001 |
https://leetcode.com/problems/rotate-array/discuss/377847/Python-multiple-solutions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | Python multiple solutions | amchoukir | 9 | 1,300 | rotate array | 189 | 0.392 | Medium | 3,002 |
https://leetcode.com/problems/rotate-array/discuss/377847/Python-multiple-solutions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
tmp = nums[-k:]
for idx in reversed(range(k, len(nums))):
nums[idx] = nums[idx-k]
for idx, num in enumerate(tmp):
nums[idx] = num | rotate-array | Python multiple solutions | amchoukir | 9 | 1,300 | rotate array | 189 | 0.392 | Medium | 3,003 |
https://leetcode.com/problems/rotate-array/discuss/377847/Python-multiple-solutions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
count = 0
start = 0
while count < len(nums):
current = start
prev = nums[start]
while True:
next = (current + k) % len(nums)
temp =... | rotate-array | Python multiple solutions | amchoukir | 9 | 1,300 | rotate array | 189 | 0.392 | Medium | 3,004 |
https://leetcode.com/problems/rotate-array/discuss/377847/Python-multiple-solutions | class Solution:
def reverse(self, nums, start, end):
while (start < end):
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
self.reverse(nums, 0, l... | rotate-array | Python multiple solutions | amchoukir | 9 | 1,300 | rotate array | 189 | 0.392 | Medium | 3,005 |
https://leetcode.com/problems/rotate-array/discuss/2673140/python3-or-easy-or-3-lines-or-99.8-faster | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k%len(nums)
n= len(nums)-k
nums[:] = nums[n:]+nums[:n] | rotate-array | python3 | easy | 3 lines | 99.8 % faster | rohannayar8 | 7 | 1,200 | rotate array | 189 | 0.392 | Medium | 3,006 |
https://leetcode.com/problems/rotate-array/discuss/2726746/Python-oror-Reversal-Algorithm-oror-Beginners-Solution-oror-Easy | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
def reverse(nums,l,h):
while l<=h:
nums[l],nums[h] = nums[h],nums[l]
l+=1
h-=1
... | rotate-array | Python || Reversal Algorithm || Beginners Solution || Easy | its_iterator | 6 | 1,200 | rotate array | 189 | 0.392 | Medium | 3,007 |
https://leetcode.com/problems/rotate-array/discuss/2078308/Python-Two-solutions-or-Easy-to-understand | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
#Approach 1
for _ in range(k):
nums.insert(0, nums.pop())
#Approach 2
k = k % len(nums)
nums[:] = nums... | rotate-array | [Python] Two solutions | Easy to understand | jamil117 | 6 | 372 | rotate array | 189 | 0.392 | Medium | 3,008 |
https://leetcode.com/problems/rotate-array/discuss/1676538/Python-simple-reverse-solution | class Solution:
def reverse(self, nums, start, end) -> None:
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
def rotate(self, nums: List[int], k: int) -> None:
k %= len(nums)
self.reverse(nums,0, len... | rotate-array | Python simple reverse solution | TovAm | 5 | 313 | rotate array | 189 | 0.392 | Medium | 3,009 |
https://leetcode.com/problems/rotate-array/discuss/2298951/Python-Easy-Solution | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | ✅Python Easy Solution | Skiper228 | 4 | 241 | rotate array | 189 | 0.392 | Medium | 3,010 |
https://leetcode.com/problems/rotate-array/discuss/1731751/Python-3-(300ms)-or-2-Fastest-Solutions-or-O(n)-and-O(1)-or-WORST-and-BEST-Solutions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
t=0
while k:
t=nums.pop(-1)
nums.insert(0,t)
k-=1 | rotate-array | Python 3 (300ms) | 2 Fastest Solutions | O(n) & O(1) | WORST & BEST Solutions | MrShobhit | 4 | 328 | rotate array | 189 | 0.392 | Medium | 3,011 |
https://leetcode.com/problems/rotate-array/discuss/1731751/Python-3-(300ms)-or-2-Fastest-Solutions-or-O(n)-and-O(1)-or-WORST-and-BEST-Solutions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
def swap(nums, start, end):
while start<end:
nums[start],nums[end]=nums[end],nums[start]
start+=1
end-=1
k=k%len(nums)
swap(nums,0,len(nums)-1)
swap(nums,0,k... | rotate-array | Python 3 (300ms) | 2 Fastest Solutions | O(n) & O(1) | WORST & BEST Solutions | MrShobhit | 4 | 328 | rotate array | 189 | 0.392 | Medium | 3,012 |
https://leetcode.com/problems/rotate-array/discuss/2228685/Python3-solution-with-very-less-execution-time-using-list-slicingoror-EXPLAINED | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k=k%len(nums)
if k==0 or k==len(nums):
return nums
nums[:]=nums[-k:]+nums[:len(nums)-k] | rotate-array | ✅Python3 solution with very less execution time using list slicing|| EXPLAINED | HarshVardhan71 | 3 | 241 | rotate array | 189 | 0.392 | Medium | 3,013 |
https://leetcode.com/problems/rotate-array/discuss/2228685/Python3-solution-with-very-less-execution-time-using-list-slicingoror-EXPLAINED | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k=k%len(nums)
if k==0 or k==len(nums):
return nums
nums=nums[-k:]+nums[:len(nums)-k]
#here nums doesn't have [:] | rotate-array | ✅Python3 solution with very less execution time using list slicing|| EXPLAINED | HarshVardhan71 | 3 | 241 | rotate array | 189 | 0.392 | Medium | 3,014 |
https://leetcode.com/problems/rotate-array/discuss/2150816/Python-Simple-And-Elegant-Solution | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
dummy = list(nums)
for i in range(0, len(nums)):
nums[ (i + k) % len(nums)] = dummy[i] | rotate-array | Python Simple And Elegant Solution | hamitg | 3 | 300 | rotate array | 189 | 0.392 | Medium | 3,015 |
https://leetcode.com/problems/rotate-array/discuss/1947653/Very-simple-Python-solution! | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k = k % len(nums)
if k != 0:
nums[:k], nums[k:] = nums[-k:], nums[:-k] | rotate-array | Very simple Python solution! | westkosing | 3 | 168 | rotate array | 189 | 0.392 | Medium | 3,016 |
https://leetcode.com/problems/rotate-array/discuss/1699359/Easy-to-understand-or-2-lines | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
nums.insert(0, nums.pop()) | rotate-array | Easy to understand | 2 lines | shandilayasujay | 3 | 175 | rotate array | 189 | 0.392 | Medium | 3,017 |
https://leetcode.com/problems/rotate-array/discuss/2661442/PYTHON-3-ONE-LINER-or-SUPER-SIMPLE | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
for x in range(k): nums.insert(0, nums[-1]), nums.pop(-1) | rotate-array | [PYTHON 3] ONE LINER | SUPER SIMPLE | omkarxpatel | 2 | 406 | rotate array | 189 | 0.392 | Medium | 3,018 |
https://leetcode.com/problems/rotate-array/discuss/2515640/PYTHON-ONE-LINE-oror-FASTER-THAN-99.99-oror-EASY-APPROACH-oror-SLICING | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k%n
nums[:] = nums[n-k:] + nums[:n-k] | rotate-array | ✅PYTHON ONE LINE || FASTER THAN 99.99% || EASY APPROACH || SLICING ✅ | bassamadnan | 2 | 199 | rotate array | 189 | 0.392 | Medium | 3,019 |
https://leetcode.com/problems/rotate-array/discuss/2463432/Python-three-different-solutions-from-worst-to-best. | class Solution:
def rotate(self, nums, k):
while k > 0: # O(k)
remove = nums.pop() # O(1)
nums.insert(0,remove) # O(n)
k-=1 | rotate-array | Python three different solutions, from worst to best. | OsamaRakanAlMraikhat | 2 | 137 | rotate array | 189 | 0.392 | Medium | 3,020 |
https://leetcode.com/problems/rotate-array/discuss/2463432/Python-three-different-solutions-from-worst-to-best. | class Solution:
def rotate(self, nums, k):
numsLength = len(nums)
numsCopy = nums.copy() # Extra n space
for i in range(0, numsLength): # O(n)
if (i+k) < numsLength:
nums[i+k] = numsCopy[i]
else:
nums[(i+k)%len(nums)] = numsCopy[i] | rotate-array | Python three different solutions, from worst to best. | OsamaRakanAlMraikhat | 2 | 137 | rotate array | 189 | 0.392 | Medium | 3,021 |
https://leetcode.com/problems/rotate-array/discuss/2463432/Python-three-different-solutions-from-worst-to-best. | class Solution:
def rotate(self, nums, k):
numsLength = len(nums)
k = k % numsLength
def reverse(l, r): # O(n)
while l < r:
temp = nums[l]
nums[l] = nums[r]
nums[r] = temp
l, r = l+1, r-1
... | rotate-array | Python three different solutions, from worst to best. | OsamaRakanAlMraikhat | 2 | 137 | rotate array | 189 | 0.392 | Medium | 3,022 |
https://leetcode.com/problems/rotate-array/discuss/2291412/EASY-PYTHON-SOLUTION-or-O(n)-Time-Complexity-or-O(1)-Space-Complexity-or | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
n = len(nums)
k = k%n
def reverse(l,r):
while l < r :
nums[l],nums[r] = nums[r],nums[l]
l += 1
r -= 1
reverse(0,n-1)
reverse(0,k-1)
... | rotate-array | EASY PYTHON SOLUTION | O(n) Time Complexity | O(1) Space Complexity | | rohitkhairnar | 2 | 166 | rotate array | 189 | 0.392 | Medium | 3,023 |
https://leetcode.com/problems/rotate-array/discuss/1756993/Python-oror-O(n)%3A-Time-oror-O(1)%3A-Space-oror-Easy-to-Understand | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
swaps, i = 0, 0
while swaps < len(nums): # we only make the necessary amount of swaps for each entry
start, num = i, nums[i] # start in... | rotate-array | Python || O(n): Time || O(1): Space || Easy to Understand | soohoonchoi | 2 | 289 | rotate array | 189 | 0.392 | Medium | 3,024 |
https://leetcode.com/problems/rotate-array/discuss/1730456/Python-3-O(n)-O(1) | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k %= n
if k == 0:
return
# reverse nums
for i in range(n // 2):
nums[i], nums[~i] = ... | rotate-array | Python 3, O(n), O(1) | dereky4 | 2 | 158 | rotate array | 189 | 0.392 | Medium | 3,025 |
https://leetcode.com/problems/rotate-array/discuss/1524905/Python-3-Simple | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
nums.insert(0,nums.pop())
return nums | rotate-array | Python 3 Simple | RashmiBhaskar | 2 | 255 | rotate array | 189 | 0.392 | Medium | 3,026 |
https://leetcode.com/problems/rotate-array/discuss/1438051/Python3-Solution-Slicing-Technique | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
""""""
Do not return anything, modify nums in-place instead.
""""""
k %= len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | Python3 Solution Slicing Technique | deleted_user | 2 | 213 | rotate array | 189 | 0.392 | Medium | 3,027 |
https://leetcode.com/problems/rotate-array/discuss/732565/Python-3Rotate-Array.-Beats-96.-Two-Liner. | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k%=len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | [Python 3]Rotate Array. Beats 96%. Two-Liner. | tilak_ | 2 | 324 | rotate array | 189 | 0.392 | Medium | 3,028 |
https://leetcode.com/problems/rotate-array/discuss/506006/PythonJSGoC%2B%2B-O(n)-by-reverse-With-comment | class Solution:
def reverse(self, nums: List[int], start, end):
# reverse array elements within [start, end] interval
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
... | rotate-array | Python/JS/Go/C++ O(n) by reverse [ With comment ] | brianchiang_tw | 2 | 557 | rotate array | 189 | 0.392 | Medium | 3,029 |
https://leetcode.com/problems/rotate-array/discuss/440602/2-liner-and-Beats-99-in-Run-time. | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
actual_rotate = k % len(nums)
nums[:] = nums[-actual_rotate:]+ nums[:-actual_rotate] | rotate-array | 2 liner and Beats 99% in Run time. | sudhirkumarshahu80 | 2 | 420 | rotate array | 189 | 0.392 | Medium | 3,030 |
https://leetcode.com/problems/rotate-array/discuss/356037/Solution-in-Python-3-(-O(1)-Space-)-(-O(n)-time-) | class Solution:
def rotate(self, n: List[int], k: int) -> None:
L, a, i, c, j, k = len(n), n[0], 0, 0, 0, k % len(n)
if L in [0,1] or k == 0: return n
while c < L:
I = (i+k)%L
a, n[I], i, c = n[I], a, I, c + 1
if i == j: a, j, i = n[j+1], j + 1, j + 1
return n
- Junaid Ma... | rotate-array | Solution in Python 3 ( O(1) Space ) ( O(n) time ) | junaidmansuri | 2 | 543 | rotate array | 189 | 0.392 | Medium | 3,031 |
https://leetcode.com/problems/rotate-array/discuss/2539461/Python-and-C-96-Beat-Simple | class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k % n
self.reverse(nums, 0, n - 1)
self.reverse(nums, 0, k - 1)
... | rotate-array | Python and C# 96% Beat Simple | estabrook | 1 | 268 | rotate array | 189 | 0.392 | Medium | 3,032 |
https://leetcode.com/problems/rotate-array/discuss/2484990/Python-(Simple-Solution-and-Beginner-Friendly) | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k%n
self.recursion(nums, 0, n-1)
self.recursion(nums, k, n-1)
self.recursion(nums, 0, k-1)
def recurs... | rotate-array | Python (Simple Solution and Beginner-Friendly) | vishvavariya | 1 | 124 | rotate array | 189 | 0.392 | Medium | 3,033 |
https://leetcode.com/problems/rotate-array/discuss/2406478/Simplest-python-solution-or-Explained-or-6-lines-or-O(n) | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
# Time complexity: O(n)
# Space complexity: O(n)
# Create another array to store rotated values
nums_rotate = [None] * len(nums)
for i in range(len(nums)):
# Th... | rotate-array | Simplest python solution | Explained | 6 lines | O(n) | harishmanjunatheswaran | 1 | 106 | rotate array | 189 | 0.392 | Medium | 3,034 |
https://leetcode.com/problems/rotate-array/discuss/2300571/Atterntion-Best-solution-with-98-faster | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
for i in range(k):
x=nums.pop()
nums.insert(0,x)
return nums | rotate-array | Atterntion ⚠️⚠️⚠️⚠️ Best solution with 98% faster | Akash2907 | 1 | 143 | rotate array | 189 | 0.392 | Medium | 3,035 |
https://leetcode.com/problems/rotate-array/discuss/2287021/Fast-or-Elegant-answer-and-simple-to-understand | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k%=len(nums)
if k==0:
return
move=current=start=0
prev=nums[start]
while move<len(nums... | rotate-array | Fast | Elegant answer and simple to understand | gagan352 | 1 | 66 | rotate array | 189 | 0.392 | Medium | 3,036 |
https://leetcode.com/problems/rotate-array/discuss/2245331/Python-Solution | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
def reverse(nums:List,beg:int, end:int):
temp = 0
while beg<end:
temp = nums[beg]
nums[beg]=nums[en... | rotate-array | Python Solution | Harshita_Tyagi | 1 | 74 | rotate array | 189 | 0.392 | Medium | 3,037 |
https://leetcode.com/problems/rotate-array/discuss/2191104/faster-than-99.90-of-Python3-online-submissions | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if k>len(nums):
k=k%len(nums)
nums[:]=nums[len(nums)-k:]+nums[:len(nums)-k] | rotate-array | faster than 99.90% of Python3 online submissions | vijayvardhan6 | 1 | 280 | rotate array | 189 | 0.392 | Medium | 3,038 |
https://leetcode.com/problems/rotate-array/discuss/2010650/Simple-2-line-solution-(faster-than-95) | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k % len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | Simple 2 line solution (faster than 95%) | vas-lag | 1 | 258 | rotate array | 189 | 0.392 | Medium | 3,039 |
https://leetcode.com/problems/rotate-array/discuss/1899999/Python-3-Simple-3-part-Reverse-Constant-Space | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
# O(n) time O(1) space
n = len(nums)
if k ==0:
return
k = k%n
self.reverse(nums, 0, n-k-1)
self.reverse... | rotate-array | Python 3 Simple 3 part Reverse Constant Space | emerald19 | 1 | 120 | rotate array | 189 | 0.392 | Medium | 3,040 |
https://leetcode.com/problems/rotate-array/discuss/1823955/My-Python-Solution-Beats-97.88-in-space. | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k %= len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | My Python Solution, Beats 97.88% in space. | Yusuf0 | 1 | 219 | rotate array | 189 | 0.392 | Medium | 3,041 |
https://leetcode.com/problems/rotate-array/discuss/1735065/Python-or-Simple-rearrangement-of-list | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k = k%len(nums)if k >= len(nums) else k
nums[:] = nums[len(nums)-k:]+nums[:len(nums)-k] | rotate-array | Python | Simple rearrangement of list | vishyarjun1991 | 1 | 96 | rotate array | 189 | 0.392 | Medium | 3,042 |
https://leetcode.com/problems/rotate-array/discuss/1706814/Two-solution-on-Python-with-x10-execution-time-difference | class Solution:
def rotate(self, nums, k):
for i in range(k):
nums.insert(0, nums.pop()) | rotate-array | Two solution on Python, with x10 execution time difference | serafinovsky | 1 | 206 | rotate array | 189 | 0.392 | Medium | 3,043 |
https://leetcode.com/problems/rotate-array/discuss/1706814/Two-solution-on-Python-with-x10-execution-time-difference | class Solution:
def rotate(self, nums, k):
k %= len(nums)
if k == 0:
return
step = len(nums) - k
left = 0
while left != len(nums) - 1 and k:
right = left + step
nums[left], nums[right] = nums[right], nums[left]
left += 1
... | rotate-array | Two solution on Python, with x10 execution time difference | serafinovsky | 1 | 206 | rotate array | 189 | 0.392 | Medium | 3,044 |
https://leetcode.com/problems/rotate-array/discuss/1611474/95.40-Faster-or-Python3-One-Liner | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
nums[:] = nums[-(k % len(nums)):] + nums[:-(k % len(nums))] | rotate-array | 95.40% Faster | Python3 One-Liner | nmax7515 | 1 | 186 | rotate array | 189 | 0.392 | Medium | 3,045 |
https://leetcode.com/problems/rotate-array/discuss/733023/Python3-a-few-approaches | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k %= len(nums)
nums[:] = nums[-k:] + nums[:-k] | rotate-array | [Python3] a few approaches | ye15 | 1 | 176 | rotate array | 189 | 0.392 | Medium | 3,046 |
https://leetcode.com/problems/rotate-array/discuss/733023/Python3-a-few-approaches | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
g = gcd(k, (n := len(nums)))
for i in range(g):
ii = i
for _ in range(n//g):
ii = (ii + k)%n
... | rotate-array | [Python3] a few approaches | ye15 | 1 | 176 | rotate array | 189 | 0.392 | Medium | 3,047 |
https://leetcode.com/problems/rotate-array/discuss/733023/Python3-a-few-approaches | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k %= len(nums)
def fn(lo, hi):
"""Reverse nums from lo (inclusive) to hi (exclusive)."""
while lo < (hi := hi - 1):
nums[lo], nums[hi] = nums[hi], nums[lo]
lo += 1
... | rotate-array | [Python3] a few approaches | ye15 | 1 | 176 | rotate array | 189 | 0.392 | Medium | 3,048 |
https://leetcode.com/problems/rotate-array/discuss/565090/Rotate-Array.-96.9-faster. | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
k%=len(nums) # If k>len(nums)
nums[:]= nums[-k:]+nums[:-k] #nums[ : ] will create a shallow copy of nums w/o taking extra space.. it's in-line pr... | rotate-array | Rotate Array. 96.9% faster. | tilak_ | 1 | 259 | rotate array | 189 | 0.392 | Medium | 3,049 |
https://leetcode.com/problems/rotate-array/discuss/246440/Output-not-showing-changed-nums | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
i = 1
while i <= k:
temp = nums[-1]
nums = nums[0:-1]
nums = [temp] + nums
i = i + 1
print(nums) | rotate-array | Output not showing changed nums | deleted_user | 1 | 531 | rotate array | 189 | 0.392 | Medium | 3,050 |
https://leetcode.com/problems/rotate-array/discuss/2838364/****Easy-to-understand-python3-code-in-just-3-line-**** | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(k):
num=nums.pop(len(nums)-1)
nums.insert(0,num) | rotate-array | ****Easy to understand python3 code in just 3 line **** | sayakmandal58 | 0 | 3 | rotate array | 189 | 0.392 | Medium | 3,051 |
https://leetcode.com/problems/rotate-array/discuss/2837136/Python-3.x | class Solution:
def rotate(self, nums: List[int], k: int) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for _ in range(k):
nums.insert(0, nums.pop()) | rotate-array | Python 3.x | SnLn | 0 | 1 | rotate array | 189 | 0.392 | Medium | 3,052 |
https://leetcode.com/problems/reverse-bits/discuss/1791099/Python-3-(40ms)-or-Real-BIT-Manipulation-Solution | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for _ in range(32):
res = (res<<1) + (n&1)
n>>=1
return res | reverse-bits | Python 3 (40ms) | Real BIT Manipulation Solution | MrShobhit | 22 | 1,800 | reverse bits | 190 | 0.525 | Easy | 3,053 |
https://leetcode.com/problems/reverse-bits/discuss/2628641/Python3-Solution-(One-Line) | class Solution:
def reverseBits(self, n: int) -> int:
return int((('{0:032b}'.format(n))[::-1]),2) | reverse-bits | Python3 Solution (One Line) | hobbabeau | 9 | 1,100 | reverse bits | 190 | 0.525 | Easy | 3,054 |
https://leetcode.com/problems/reverse-bits/discuss/2440929/C%2B%2BPython-Optimized-O(N)-Solution | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
res = (res<<1) + (n&1)
n>>=1
return res | reverse-bits | C++/Python Optimized O(N) Solution | arpit3043 | 4 | 267 | reverse bits | 190 | 0.525 | Easy | 3,055 |
https://leetcode.com/problems/reverse-bits/discuss/1592120/WEEB-DOES-PYTHON | class Solution:
def reverseBits(self, n: int) -> int:
binary = list(bin(n))[2:] # we don't need 0b
remainder = ["0"]
zerosNeeded = 32 - len(binary)
newBinary = binary[::-1] + remainder * zerosNeeded # add the missing zeros
return int("".join(newBinary), 2) | reverse-bits | WEEB DOES PYTHON | Skywalker5423 | 4 | 327 | reverse bits | 190 | 0.525 | Easy | 3,056 |
https://leetcode.com/problems/reverse-bits/discuss/1684479/2-solutions-in-python-(single-line-and-3-lines-python-solution) | class Solution:
def reverseBits(self, n: int) -> int:
b=bin(n).replace("0b","")[::-1]
extra="".join([str(0)]*(32-len(b)))
return int(b+extra,2) | reverse-bits | 2 solutions in python (single line and 3 lines python solution) | amannarayansingh10 | 3 | 276 | reverse bits | 190 | 0.525 | Easy | 3,057 |
https://leetcode.com/problems/reverse-bits/discuss/1684479/2-solutions-in-python-(single-line-and-3-lines-python-solution) | class Solution:
def reverseBits(self, n: int) -> int:
return int(bin(n).replace("0b","")[::-1]+"".join([str(0)]*(32-len(bin(n).replace("0b","")))),2) | reverse-bits | 2 solutions in python (single line and 3 lines python solution) | amannarayansingh10 | 3 | 276 | reverse bits | 190 | 0.525 | Easy | 3,058 |
https://leetcode.com/problems/reverse-bits/discuss/2473342/Python-Beats-98-Short-Easy-and-Faster-Solution-oror-Documented | class Solution:
def reverseBits(self, n: int) -> int:
n = '{:032b}'.format(n) # convert into to binary string
n = n[::-1] # reverse string
n = int(n, 2) # convert string into integer base 2
return n | reverse-bits | [Python] Beats 98% Short, Easy and Faster Solution || Documented | Buntynara | 2 | 144 | reverse bits | 190 | 0.525 | Easy | 3,059 |
https://leetcode.com/problems/reverse-bits/discuss/2250299/Python-with-full-working-explanation | class Solution:
def reverseBits(self, n: int) -> int: # Time: O(1) and Space: O(1)
ans = 0
for i in range(32):
ans = (ans << 1) + (n & 1)
n >>= 1
return ans | reverse-bits | Python with full working explanation | DanishKhanbx | 2 | 173 | reverse bits | 190 | 0.525 | Easy | 3,060 |
https://leetcode.com/problems/reverse-bits/discuss/2180381/Python-easiest-one-liner | class Solution:
def reverseBits(self, n: int) -> int:
return int(str(bin(n)[2:]).zfill(32)[::-1], 2) | reverse-bits | Python easiest one liner | pro6igy | 2 | 196 | reverse bits | 190 | 0.525 | Easy | 3,061 |
https://leetcode.com/problems/reverse-bits/discuss/1777165/Python-Simple-Python-Solution-Using-Binary-Number | class Solution:
def reverseBits(self, n: int) -> int:
binary_number = bin(n)[2:]
binary_number = '0'*(32-len(binary_number))+binary_number
reverse_binary_number = binary_number[::-1]
return int(reverse_binary_number, 2) | reverse-bits | [ Python ] ✔✔ Simple Python Solution Using Binary Number 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 2 | 367 | reverse bits | 190 | 0.525 | Easy | 3,062 |
https://leetcode.com/problems/reverse-bits/discuss/749391/Easy-solution-with-Python-94 | class Solution:
def reverseBits(self, n: int) -> int:
return(int(bin(n)[2:].zfill(32)[::-1],2)) | reverse-bits | Easy solution with Python 94% | NvsYashwanth | 2 | 363 | reverse bits | 190 | 0.525 | Easy | 3,063 |
https://leetcode.com/problems/reverse-bits/discuss/2718241/FASTEST-AND-EASIEST-oror-LEFT-SHIFT-AND-BINARY-DECIMAL-CONVERSION-oror-FASTEST | class Solution:
def reverseBits(self, n: int) -> int:
binary = bin(n)[2:]
rev = binary[::-1]
reverse = rev.ljust(32, '0')
return int(reverse, 2) | reverse-bits | FASTEST AND EASIEST || LEFT SHIFT AND BINARY-DECIMAL CONVERSION || FASTEST | Pritz10 | 1 | 16 | reverse bits | 190 | 0.525 | Easy | 3,064 |
https://leetcode.com/problems/reverse-bits/discuss/2655992/python | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
if n >> 1 << 1 != n:
n = n >> 1
res += pow(2,31-i)
else:
n = n >> 1
return res | reverse-bits | python | zoey513 | 1 | 243 | reverse bits | 190 | 0.525 | Easy | 3,065 |
https://leetcode.com/problems/reverse-bits/discuss/2540603/Python3-runtime-faster-than-96.69-memory-less-than-94.08-basic-one-liner | class Solution:
def reverseBits(self, n: int) -> int:
return int("{:032b}".format(n)[::-1], 2) | reverse-bits | Python3 - [runtime faster than 96.69%, memory less than 94.08%] basic one liner | kamildoescode | 1 | 150 | reverse bits | 190 | 0.525 | Easy | 3,066 |
https://leetcode.com/problems/reverse-bits/discuss/2387445/99.75-FASTER-oror-VIDEO-EXPLANATION | class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
res=0
for i in range(32):
bit=(n>>i)&1
res=res|bit<<(31-i)
return res | reverse-bits | 99.75% FASTER || VIDEO EXPLANATION | Egan_707 | 1 | 131 | reverse bits | 190 | 0.525 | Easy | 3,067 |
https://leetcode.com/problems/reverse-bits/discuss/2233530/Python-3-lines-of-code | class Solution:
def reverseBits(self, n: int) -> int:
reverseStr = str(bin(n))[::-1][:-2]
reverseStr += (32-len(reverseStr)) * '0'
return int(reverseStr, 2) | reverse-bits | Python, 3 lines of code | franzlleshaj | 1 | 128 | reverse bits | 190 | 0.525 | Easy | 3,068 |
https://leetcode.com/problems/reverse-bits/discuss/2153211/Easy-Python-string-manipulation | class Solution:
def reverseBits(self, n: int) -> int:
s = str(bin(n))[2:][::-1]
while len(s) != 32:
s += '0'
return int(s, 2) | reverse-bits | Easy Python string manipulation | knishiji | 1 | 194 | reverse bits | 190 | 0.525 | Easy | 3,069 |
https://leetcode.com/problems/reverse-bits/discuss/1856218/Python-or-Easy-Understanding | class Solution:
def reverseBits(self, n: int) -> int:
result = 0
for _ in range(32):
result <<= 1
result += n & 1
n >>= 1
return result | reverse-bits | Python | Easy-Understanding | Mikey98 | 1 | 328 | reverse bits | 190 | 0.525 | Easy | 3,070 |
https://leetcode.com/problems/reverse-bits/discuss/1822455/Best-Python-Solution.-EASY-TO-UNDERSTAND. | class Solution:
def reverseBits(self, n: int) -> int:
i, new = 0, 0;
while n >> i:
if (n >> i) & 1:
new |= 1 << (31-i);
i += 1;
return new; | reverse-bits | Best Python Solution. EASY TO UNDERSTAND. | zeyf | 1 | 187 | reverse bits | 190 | 0.525 | Easy | 3,071 |
https://leetcode.com/problems/reverse-bits/discuss/1665306/Python-bitwise-shift | class Solution:
def reverseBits(self, n: int) -> int:
result = 0
for _ in range(32):
result <<= 1
result |= n & 1
n >>= 1
return result | reverse-bits | Python, bitwise shift | blue_sky5 | 1 | 91 | reverse bits | 190 | 0.525 | Easy | 3,072 |
https://leetcode.com/problems/reverse-bits/discuss/1615400/Python3-over-97-without-messing-around-with-strings-O(orbitsor) | class Solution:
def reverseBits(self, n: int) -> int:
a = 0
for i in range(32):
a = (a << 1) + n % 2
n = (n >> 1)
return a | reverse-bits | Python3 over 97% without messing around with strings - O(|bits|) | ctonic | 1 | 113 | reverse bits | 190 | 0.525 | Easy | 3,073 |
https://leetcode.com/problems/reverse-bits/discuss/1159028/Python3-Single-Line-And-Simple-Solution | class Solution:
def reverseBits(self, n: int) -> int:
return int(bin(n)[2:].rjust(32 , '0')[::-1] , 2) | reverse-bits | [Python3] Single Line And Simple Solution | Lolopola | 1 | 112 | reverse bits | 190 | 0.525 | Easy | 3,074 |
https://leetcode.com/problems/reverse-bits/discuss/2831361/python-oror-simple-solution-oror-one-liner | class Solution:
def reverseBits(self, n: int) -> int:
'''
1) get binary representation of n
2) strip of "0b" at the front, reverse it
3) align the string to the left and fill the right with 0s to make it len 32
4) convert to int with base 2
'''
return int(bin(... | reverse-bits | python || simple solution || one-liner | wduf | 0 | 8 | reverse bits | 190 | 0.525 | Easy | 3,075 |
https://leetcode.com/problems/reverse-bits/discuss/2765958/Easy-Python3-solution | class Solution:
def reverseBits(self, n: int) -> int:
# n is given as integer: convert to binary first
s = str(bin(n).replace("0b", ""))
# insert in beginning - zeroes to make it 32 bits
while len(s) != 32:
s = '0'+s
# now we have the binary ... | reverse-bits | Easy Python3 solution | sourav_ravish | 0 | 5 | reverse bits | 190 | 0.525 | Easy | 3,076 |
https://leetcode.com/problems/reverse-bits/discuss/2740599/Single-line-solution | class Solution:
def reverseBits(self, n: int) -> int:
return int('{0:032b}'.format(n)[::-1],2) | reverse-bits | Single line solution | Raghunath_Reddy | 0 | 8 | reverse bits | 190 | 0.525 | Easy | 3,077 |
https://leetcode.com/problems/reverse-bits/discuss/2728213/Python-(single-Line-Solution) | class Solution:
def reverseBits(self, n: int) -> int:
return int((('{0:032b}'.format(n))[::-1]),2) | reverse-bits | Python (single Line Solution) | durgaraopolamarasetti | 0 | 5 | reverse bits | 190 | 0.525 | Easy | 3,078 |
https://leetcode.com/problems/reverse-bits/discuss/2727996/Python-O(1)-answer | class Solution:
def reverseBits(self, n: int) -> int:
if n == 0:
return 0
out = 0 # Initialize output with '0000...{32 times}...000'
for _ in range(31):
bit = n & 1 # 1. Take the last bit of n
out |= bit # 2. Set the last bit of output to the last bit of n
out <<= 1 # 3. Left shift outp... | reverse-bits | Python O(1) answer | sarthakpandey1210 | 0 | 13 | reverse bits | 190 | 0.525 | Easy | 3,079 |
https://leetcode.com/problems/reverse-bits/discuss/2716007/Bit-Manipulation-Solution-Fast-and-Easy | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
bit = (n >> i) & 1
res = res | bit << (31 - i)
return res | reverse-bits | Bit Manipulation Solution Fast and Easy | user6770yv | 0 | 5 | reverse bits | 190 | 0.525 | Easy | 3,080 |
https://leetcode.com/problems/reverse-bits/discuss/2685664/easy-code!!!! | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
res = (res<<1) + (n&1)
n>>=1
return res | reverse-bits | easy code!!!! | sanjeevpathak | 0 | 6 | reverse bits | 190 | 0.525 | Easy | 3,081 |
https://leetcode.com/problems/reverse-bits/discuss/2661615/Python-simple-solution | class Solution:
def reverseBits(self, n: int) -> int:
temp = bin(n).replace("0b","")
l = len(temp)
temp = temp[::-1]
if l<32:
for i in range(32-l):
temp +='0'
return int(temp,2) | reverse-bits | Python simple solution | parthdixit | 0 | 56 | reverse bits | 190 | 0.525 | Easy | 3,082 |
https://leetcode.com/problems/reverse-bits/discuss/2660749/python3-easy-solu-wid-explaination-using-bit-magic | class Solution:
def reverseBits(self, n: int) -> int:
i=0
res=0
while i<32:
b=n&1 #to check last bit is 1 or 0
r=b<<(31-i) #inserting the b at 31-i position eg.in first itertion for n=5
#5=(101) when done & with 1 then b=1 and 1 will be in... | reverse-bits | python3 easy solu wid explaination using bit magic | tush18 | 0 | 27 | reverse bits | 190 | 0.525 | Easy | 3,083 |
https://leetcode.com/problems/reverse-bits/discuss/2628993/Python-oror-Fast-and-Easy | class Solution:
def reverseBits(self, n: int) -> int:
b = str(bin(n))[2:]
b = '0'*(32 - len(b)) + b
return int(b[::-1],2)
``` | reverse-bits | Python || Fast and Easy | trasherr | 0 | 34 | reverse bits | 190 | 0.525 | Easy | 3,084 |
https://leetcode.com/problems/reverse-bits/discuss/2612974/easy-python3-solution | class Solution:
def reverseBits(self, n: int) -> int:
binary_n = bin(n)
s = list(binary_n)[2::][::-1]
i = len(s)
while i < 32:
s.append('0')
i += 1
return int(''.join(s), 2) | reverse-bits | easy python3 solution | codeSheep_01 | 0 | 99 | reverse bits | 190 | 0.525 | Easy | 3,085 |
https://leetcode.com/problems/reverse-bits/discuss/2562616/Python-one-liner-with-explanation | class Solution:
def reverseBits(self, n: int) -> int:
return int(f"{n:#034b}"[::-1][:-2],2) | reverse-bits | Python one-liner with explanation | ComputerWiz | 0 | 116 | reverse bits | 190 | 0.525 | Easy | 3,086 |
https://leetcode.com/problems/reverse-bits/discuss/2456205/Python-7ms-solution-(one-liner) | class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
return int(str((bin(n)[2:]).zfill(32)[::-1]),2) | reverse-bits | Python 7ms solution (one liner) | wojtih123 | 0 | 168 | reverse bits | 190 | 0.525 | Easy | 3,087 |
https://leetcode.com/problems/reverse-bits/discuss/2449572/python3-one-line-with-Explanation | class Solution:
def reverseBits(self, n: int) -> int:
return int(str(bin(n)[2:].zfill(32))[::-1],2) | reverse-bits | python3 one line with Explanation | vilkinshay | 0 | 69 | reverse bits | 190 | 0.525 | Easy | 3,088 |
https://leetcode.com/problems/reverse-bits/discuss/2436240/One-Line-Answer-in-Python | class Solution:
def reverseBits(self, n: int) -> int:
return int(bin(n)[2:].zfill(32)[::-1], 2) | reverse-bits | One-Line-Answer in Python | dasistmoin123 | 0 | 85 | reverse bits | 190 | 0.525 | Easy | 3,089 |
https://leetcode.com/problems/reverse-bits/discuss/2432007/Python3-Short-solution-%2B-Broken-down-step-by-step-w-explanation | class Solution:
def reverseBits(self, n: int) -> int:
# Short Version
out = str(bin(n))[::-1][:-2]
while len(out) < 32:
out += '0'
return int(out, 2)
# Long, step-by-step version
# First, let's see the binary of the original number
#print(bin(n))... | reverse-bits | [Python3] Short solution + Broken down step-by-step w explanation | connorthecrowe | 0 | 126 | reverse bits | 190 | 0.525 | Easy | 3,090 |
https://leetcode.com/problems/reverse-bits/discuss/2378395/python-easy-5-Line | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
bit = (n >> i) & 1
res = res | (bit << (31 - i))
return res | reverse-bits | python easy 5 Line | soumyadexter7 | 0 | 106 | reverse bits | 190 | 0.525 | Easy | 3,091 |
https://leetcode.com/problems/reverse-bits/discuss/2290838/Python-or-Easy-to-Understand | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
bit = n>>i & 1
res = res | bit<<(32-1-i)
return res | reverse-bits | Python | Easy to Understand | Abhi_009 | 0 | 171 | reverse bits | 190 | 0.525 | Easy | 3,092 |
https://leetcode.com/problems/reverse-bits/discuss/2275509/Python-1-liner | class Solution:
def reverseBits(self, n: int) -> int:
return int('{0:032b}'.format(n)[::-1],2) | reverse-bits | Python 1 - liner | Vaibhav7860 | 0 | 103 | reverse bits | 190 | 0.525 | Easy | 3,093 |
https://leetcode.com/problems/reverse-bits/discuss/2223541/Python-Easy-to-Understand | class Solution:
def reverseBits(self, n: int) -> int:
res = ""
for i in range(31, -1, -1):
res = str(n // (2**i)) + res
n = n % (2**i)
return int(res, 2) | reverse-bits | Python Easy to Understand | codeee5141 | 0 | 62 | reverse bits | 190 | 0.525 | Easy | 3,094 |
https://leetcode.com/problems/reverse-bits/discuss/2088771/Simple-python-program | class Solution:
def reverseBits(self, n: int) -> int:
reverseBits = 0
for i in range(32):
remainder = n % 2
n = n // 2
reverseBits = reverseBits * 2 + remainder
return reverseBits | reverse-bits | Simple python program | rahulsh31 | 0 | 123 | reverse bits | 190 | 0.525 | Easy | 3,095 |
https://leetcode.com/problems/reverse-bits/discuss/1996984/Python-easy-solution | class Solution:
def reverseBits(self, n: int) -> int:
res = 0
for i in range(32):
res = res << 1
bit = n%2
res += bit
n = n >> 1
return res | reverse-bits | Python easy solution | dbansal18 | 0 | 59 | reverse bits | 190 | 0.525 | Easy | 3,096 |
https://leetcode.com/problems/reverse-bits/discuss/1692070/Python3-Solution | class Solution:
def reverseBits(self, n: int) -> int:
result=0
for i in range(32):
bit = (n>>i) & 1
result = result | (bit<<(31-i))
return result | reverse-bits | Python3 Solution | nomanaasif9 | 0 | 147 | reverse bits | 190 | 0.525 | Easy | 3,097 |
https://leetcode.com/problems/reverse-bits/discuss/1635252/Easy-Bitwise-Solution-Python | class Solution:
def reverseBits(self, n: int) -> int:
ans = 0
bits = 31
while n:
lsb = n & 1
ans = ans | (lsb << bits)
bits -= 1
n = n >> 1
return ans | reverse-bits | Easy Bitwise Solution Python | dahal_ | 0 | 170 | reverse bits | 190 | 0.525 | Easy | 3,098 |
https://leetcode.com/problems/reverse-bits/discuss/1628297/Python-oror-With-comments-oror-28ms | class Solution:
def reverseBits(self, A: int) -> int:
#convert int to binary in reverse
rev = ""
while A > 0 :
rev += str(A % 2)
A = A // 2
rev = rev.ljust(32,'0')
#rev is the reversed binary of A, now we need to change it to decimal
res = 0
... | reverse-bits | Python || With comments || 28ms | s_m_d_29 | 0 | 144 | reverse bits | 190 | 0.525 | Easy | 3,099 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.