Problem
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.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Bubble Zeroes to the End
Here we keep note of the index, where we find the zero and bubble it out with not zero element.
Code
|
|
Method 2 - Using Partition in Quicksort OR Two Pointer 🏆
Actually, we can use the similar code that is used to solve
We can use almost identical code to solve those problems!
Code
|
|