Welcome to my world!
Difficulty: Easy
Tags: Array, Two Pointers
将一个整型数组中所有的0都移到数组最后。
模拟题。用vector.erase()和push_back()即可,注意控制下标。
▷ Time Complexity: O(n)
▷ Space Complexity: O(1)
1234567891011121314151617
class Solution {public: void moveZeroes(vector<int>& nums) { int length = nums.size(); for(int i=0; i<length; i++) { if(nums[i] == 0) { nums.erase(nums.begin() + i); nums.push_back(0); i--; length--; } } }};
回到 Conquer Leetcode