Move Zeros Python Leetcode 283

preview_player
Показать описание


🔴 Question Link -

✅Connect with me

🔴 Question with Example:
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Рекомендации по теме
Комментарии
Автор

We can use the shorthand for i in range(len(nums)) instead of for i in range(0, len(nums)), and write nums[i], nums[prev_indx] = nums[prev_indx], nums[i] instead of creating a temporary, holding variable. I'm sure you already know this by now, but just wanted to put that out there for everyone else. Gotta love Python!

mshutube
Автор

def move_zeros(array):
for i in array:
if i == 0:
array.remove(i) # Remove the element from the array
array.append(i) # Append the element to the end
return array

lukamoz
Автор

def move_zeros(nums):
# Count the number of zeros
zero_count = nums.count(0)

# Remove all zeros from the list
nums = [num for num in nums if num != 0]

# Append the required number of zeros at the end
nums.extend([0] * zero_count)

# Return the modified array
return nums

nums = [0, 1, 0, 3, 12]
result = move_zeros(nums)
print(result)

betenu
Автор

input = [0, 1, 0, 3, 12]

i = 0 #Declare an iterable

for i in range(0, len(input)):
if input[i] == 0:
input.pop(i)
input.insert(len(input), 0)

print(input)

prakash
Автор

One line and 0ms runtime:

class Solution:

def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
nums[:] = [i for i in nums if i != 0] + [0 for i in nums if i == 0]

Guillermo-zpou
Автор

Lol this took me such a long time 😂 it was the "in-place" part that got me.

Vancha
Автор

lst = [0, 1, 0, 0, 1, 4, 5]

k = lst.count(0)
for i in range(k):
lst.remove(0)
lst.append(0)
print(lst)

jbn
Автор

for index, value in enumerate(input):
if value == 0:
input.pop(index)
input.append(0)

print(input)

milindabackup
Автор

you can swap in one line in python. num[i], num[prev_idx]=num[prev_idx], num[i]

anildangol
Автор

def s_sort(nums):
for i in range(len(nums)-1, 0, -1):
for j in range(i):
if nums[j]==0:
temp=nums[j]
nums[j]=nums[j+1]
nums[j+1]=temp

list1=[0, 1, 0, 3, 12]
s_sort(list1)
print(list1)

mukundbiradar
Автор

def transfer(li0):
a = []
b = []
for i in li0:
if i != 0:
a.append(i)
else:
b.append(i)
return a + b

print (transfer([1, 0, 2, 0, -3, 4]))

duaamelhem
Автор

for i in range(len(l)):
if l[i]==0:
l.remove(0)
l.insert(len(l), 0)
else:
continue

rashmidutta
Автор

for idx, i in enumerate(input_list):
if i == 0:
input_list += [input_list.pop(idx)]

armans
Автор

for i in range(len(nums)):
if nums[i] != 0:
index = nums.index(0)
nums[i], nums[index] = nums[index], nums[i]

EduardoAstorga
Автор

def funSwap (nums):
for i in range (0, len(nums)):
if nums[i] == 0 :
#Go till you find the next non zero integer
nextNonZeroNumIndex = i
for j in range (i, len(nums)) :
if nums[j] != 0 :
nextNonZeroNumIndex = j
break

#here comes the swap logic
nums[i] = nums[nextNonZeroNumIndex]
nums[nextNonZeroNumIndex] = 0

print (nums)

priyashah
Автор

l =[0, 1, 0, 3, 12]
q= []
count = 0
for i in l:
if i != 0:

q.append(i)

elif i == 0:
count += 1
q.extend([0]*count)

print(q)

hritchi
Автор

Thanks for making great videos like this, can you please make a complete playlist of all the pyrhon - leetcode problems

Sakshamjn
Автор

Hi, great video just have one question. If an array is [1, 2, 3, 0, 12] so first three elements are non 0 and our code is that we swap left value with the right value every time when right value is no zero, so when we start from 1 and next value is 2, that means that our array will look like [2, 1 ...] and order od non 0 elements will be changed. Should we specify in code that we do swap only when left value is 0 and right not ? Thanks

Marko
Автор

def MoveZe (input) for i in range (len[input]) if I == 0 input.remove(0) input.append () return input print (MoveZe(input))

Prasadsway
Автор

l=[0, 1, 0, 3, 12]
print([x for x in l if x!=0]+ [0]*l.count(0))

ManishKumarSingh-mr
welcome to shbcf.ru