C++ solution, swap in place (accepted)


  • 1
    S
      if (nums.size() == 0) return;
    
      int zero_count=0;
      int index=0;
    
      //Move nonzeros to front
      vector<int>::iterator it;
      for (it = nums.begin(); it != nums.end(); ++it){
         if (!(*it)) zero_count++;
         else{
           nums[index]=*it;
           index++;
         }
      }
    
      //Append end with zeros
      for (int i = index; 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.