题目
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.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
问题陈述:
把给定数组中的0全部挪到后边,保持其余数字顺序不变
题目思路:
遍历,把非零元素从前向后赋值给原数组,剩余空位补0。
代码:
|
结果