carsonSgit / leetcode

leetcode grind... doing whatever I can to make sure I'm not bad at CS.
4 stars 0 forks source link

Q283 Solution #2

Closed carsonSgit closed 4 months ago

carsonSgit commented 4 months ago

What is this 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.

How do I solve it

Breakdown:

  1. Create the initial index insert_index.
  2. Go through the nums array and shift all non-zero values to the left.
  3. Once all non-zero values have been shifted, fill the rest of the nums array with zeros.