Python 1liner using inplace sort


  • 7
    S
    class Solution(object):
        def moveZeroes(s, a):
            a.sort(key=lambda x: not x or None)

  • 1

    Nice. Though do you know how large the total number of operations is? (I don't, and don't have time to research it).

    Also, why the or None? Even this works:

    def moveZeroes(self, nums):
        nums.sort(key=operator.not_)
    

  • 0
    S

    Oh, sure, you're right. I didn't spent any time polishing it.

    As for the complexity, that's a good question, I'm not sure what it is; but I know it only works because the default sorting in Python is stable.


Log in to reply
 

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