Straightforward O(n) c++ solution.


  • 2
    H
    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            int next = 0;
            for (auto n : nums) {
                if (n != 0) nums[next++] = n;
            }
            fill(begin(nums)+next, end(nums), 0);
        }
    };

Log in to reply
 

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