Python 1liner using inplace sort

  • 8
    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):

  • 0

    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.