C++ stable_partition() pretty much does exactly this


  • 0
    M

    The std::partition() function will move the zeros, but it might change relative order of the other elements. std::stable_partition() retains relative order.

        class Solution {
        public:
            void moveZeroes(vector<int>& nums) {
                stable_partition(
                    begin(nums), end(nums),
                    [](int val) { return val != 0; });
            }
        };
    

Log in to reply
 

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