Python One Liner


  • 0
    C

    Hello all, this is my first post here.
    I am using the list comprehension.

    class Solution(object):
        def singleNonDuplicate(self, nums):
            return [nums[i] for i in range(len(nums)) if nums.count(nums[i]) < 2][0]
    

  • 0
    C

    However, it finished outside of the time limit :(


  • 0
    P

    More convenient way :

    return sum(set(nums))*2-sum(nums)
    

  • 0
    H

    Hi, I am wondering the time complexity of sum(nums), I know this solution can be accepted, but the time complexity should be O(n), while the requirement is O(logn). Also, the time and space complexities of set(nums) are also larger than O(logn) and O(1).


  • 0
    S

    @clemsonc expected solution should be O(logn)


Log in to reply
 

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