Hey dev! In case you don't know, I've started #45daysofleetcode and in these 45 days i'll be writing about two problems every day.
The problem, how I solved it and a bit of detailed explanation.
I'm sharing this progress with you so that you too can learn a bit of different perspective and have new solutions so hit like to join this journey.
I've chosen the best list of problems from medium to hard. That made lots of developers crack the interview.
Are you excited?
Today's Problem
Name - Move Zeros
Description - Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example -
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
My Approach - I will iterate through the list and when found a zero in the path take it and append it to the list and remove it.
When you find a zero in the path, you append it to the list and then remove it from the path. Simple.
Code -
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(len(nums)):
if nums[i] == 0:
nums.append(nums[i])
nums.remove(nums[i])
Let me know if you've any questions or suggestions!.
Follow to join this #45daysofleetcode
Full list of solutions are found in this repo. make sure you star it.
Bye!
Top comments (0)