C++ Accepted Code, 16ms


  • 0
    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            if (nums.empty()) return;
            int count = 0;
            int size = nums.size();
            for (int i = 0; i < size; ++i) {
                if (0 == nums[i]) ++count; // record zeros numbers
                else nums[i - count] = nums[i]; // move none-zero item
            }
            for (int i = size - count; i < size; ++i) {
                nums[i] = 0; // fill zero
            }
        }
    };
    

Log in to reply
 

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