Skip to content

Commit c79d468

Browse files
committed
Adding Efficient Solution for Problem - 283 Move Zeroes
1 parent 37f28e3 commit c79d468

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Move_Zeroes/EfficientSolution.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 283
6+
## Problem Name: Move Zeroes
7+
##===================================
8+
#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.
9+
#
10+
#Example:
11+
#
12+
#Input: [0,1,0,3,12]
13+
#Output: [1,3,12,0,0]
14+
class Solution:
15+
def moveZeroes(self, nums):
16+
index = 0 #Initialize index
17+
for i in range(len(nums)): #Loop through nums
18+
if nums[i] != 0: #Condition-check: if nums[i] is not equal to zero.
19+
nums[index], nums[i] = nums[i], nums[index] #Swap the values so that zero will go at the end of list.
20+
index += 1 #Increase index by one

0 commit comments

Comments
 (0)