Radix sort in python


  • 0
    Z
    class Solution(object):
        def longestConsecutive(self, nums):
            def getDigit(number, k):
                return (number / pow(10, k)) % 10
            def radix_sort(nums):
                for k in range(10):
                    buckets = [[] for _ in range(10)]
                    for num in nums:
                        buckets[getDigit(num, k)].append(num)
                    nums = []
                    for bucket in buckets:
                        for b in bucket:
                            nums.append(b)
                return [num for num in nums if num < 0] + [num for num in nums if num >= 0]
            def remove_dupicates(nums):
                num_set = set()
                num_list = []
                for num in nums:
                    if num not in num_set:
                        num_list.append(num)
                        num_set.add(num)
                return num_list
            nums = remove_dupicates(radix_sort(nums))
            i, c, max_c = 0, 0, 0
            while i < len(nums):
                if nums[i] != (nums[i-1]+1 if i > 0 else None):
                    c = 1
                else:
                    c += 1
                max_c = max(max_c, c)
                i += 1
            return max_c

Log in to reply
 

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