24ms Answer in C++


  • 0
    W
    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            int pt = 0;
            if (nums.size()==0 || nums.size()==1) return;
            for (int i=0;i<nums.size();i++){
                if (nums[i]!=0){
                    nums[pt] = nums[i];
                    pt++;
                }
            }
            for (int i=pt;i<nums.size();i++) nums[i]=0;
        }
    };

  • 0
    G

    the same method in JAVA cost me 312ms


Log in to reply
 

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