A Straightforward Solution


  • 5
    I
    class Solution 
    {
        public:
        void moveZeroes(vector<int>& nums) 
        {
            if (nums.empty()) return;
            int j = 0;
            for (int i = 0; i < nums.size(); i++)
            {
                if (nums[i] != 0)
                {
                    nums[j++] = nums[i];
                }
            }
            for (int i = j; i < nums.size(); i++)
            {
                nums[i] = 0;
            }
        }
    };

Log in to reply
 

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