Python 6-line solution, no set, beats 95%

  • 0
    class Solution(object):
        def subsetsWithDup(self, nums):
            :type nums: List[int]
            :rtype: List[List[int]]
            def helper(arr):
                if not arr: return [[]]
                num, i = arr[0], 1
                while i < len(arr) and arr[i] == num: i += 1
                return [[num]*l + subset for l in xrange(i+1) for subset in helper(arr[i:])]
            return helper(sorted(nums))

Log in to reply

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