Problem:
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.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
-Summary-
Used two-pointer using two loops.
1. set 1 pointer of for loop for the zero-value index in the nums list.
2. If zero index found, then we loop starting from the next index then find the number that is not zero.
3. Once the number found, we swap the value with zero and break. Then Keep continuing the loop
4. Once the loop finished, all the zero will be at the end of the list and other numbers will be staying in the relative order.
모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]
'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글
LeetCode 122. Best Time to Buy and Sell Stock II (1) | 2020.05.23 |
---|---|
LeetCode 48. Rotate Image (0) | 2020.05.22 |
LeetCode 189. Rotate Array (0) | 2020.05.22 |
LeetCode 01. Two Sum (0) | 2020.05.21 |
LeetCode 36. Valid Sudoku [Medium] (2) | 2020.05.20 |
댓글