From linkjobai, 1 Week ago, written in Plain Text.
Embed
  1. Move Zeroes (LeetCode 283): Interview Notes
  2.  
  3. Problem
  4. Given an integer array nums, move all 0's to the end while keeping the relative order of the non-zero elements. Do this in-place.
  5.  
  6. Approach
  7. Two pointers: write index w starts at 0. Scan read index r; whenever nums[r] != 0, write it to nums[w] and increment w. After the scan, fill nums[w:] with zeros. Narrate a dry run on [0,1,0,3,12].
  8.  
  9. Complexity
  10. Time O(n), space O(1).
  11.  
  12. Practice tip: https://www.linkjob.ai/
  13.  
  14. Disclosure: I work on Linkjob AI.
  15.