136. Single Number

 Difficulty: Easy

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

Example 1:

Input: nums = [2,2,1]
Output: 1

Example 2:

Input: nums = [4,1,2,1,2]
Output: 4

Example 3:

Input: nums = [1]
Output: 1
Python Solution:

class Solution(object):
    def singleNumber(self, nums):
        nums.sort()
        i=0
        while 1:
            try:
                if nums[i]==nums[i+1]:
                    del nums[i]
                    del nums[i]
                else:
                    i+=1
            except:
                break
        return nums[0]

Comments

Popular posts from this blog

35. Search Insert Position

118. Pascal's Triangle

763. Partition Labels