C++ 20ms ,using erase and iterator


  • 0
    B

    class Solution {
    public:
    void moveZeroes(vector<int>& nums) {
    int length = 0;
    vector<int>::iterator it;
    for(it = nums.begin() ; it !=nums.end();it++)
    {
    if(*it == 0)
    {
    nums.erase(it);
    length++;
    it--;
    }
    }
    while(length--)
    {
    nums.push_back(0);
    }

    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.