Leetcode 283: Move Zeroes
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移动到数组尾部,而非零的数保持一定的顺序。比如,给一个数组nums =[0, 1, 0, 3, 12]
,经过该函数处理后,nums应该变为[1, 3, 12, 0, 0]
;
程序(C语言)
|
|
结果
21 / 21 test cases passed.
Status: Accepted
Runtime: 8 ms