题目描述
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:
1 | Input: [0,1,0,3,12] |
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
题目要求inplace without making a copy对两个索引从左到右进行遍历,第一个索引index1遍历到0元素就停下,然后第二个索引index2继续遍历,直到第一个非零元素且index2>index1就停下,然后交换元素的值即可。
代码实现
1 | class Solution: |