4 Lines C++ Solution , 16ms (Explanation also given)


  • 2
    K

    Let me tell you how this works:
    We take 0 as our pivot ,like in quicksort. All the elements which are not 0 are swapped with the number of index 'j'. So, automatically , all the zeroes will be at the end of the array.

    int i=-1,j=0;
    while((++i)<nums.size())
          if(!(nums[i]==0))
               swap(nums[i],nums[j++]);

  • 0
    X

    @karan.vas1994 but why it runs so quick?


Log in to reply
 

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