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.
lc in cpp
1 |
void (vector<int>& nums) { |
题目要求O(1)space,那就用双指针,左边指向0,右边指向非零.当右边指到非零的时候,交换当前指向零的数即可.
近期评论