题目描述:(链接):
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:
解题思路:
类似于Remove Elements。
1 class Solution { 2 public: 3 void moveZeroes(vector<int>& nums) { 4 int n = nums.size(); 5 int j = 0; 6 for (int i = 0; i < n; ++i) { 7 if (nums[i] != 0) { 8 nums[j++] = nums[i]; 9 } 10 } 11 12 if (j != 0) { 13 fill(nums.begin() + j, nums.end(), 0); 14 } 15 } 16 };
原文:http://www.cnblogs.com/skycore/p/4889830.html