136. Single Number; 137. Single Number II;260. Single Number III; 287. Find the Duplicate Number

#136. Single Number
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
single=[]
for i in range(len(nums)):
if nums[i] not in single:
single.append(nums[i] )
else:
single.remove(nums[i])
return single[0]

class Solution(object):
    def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hash_table={}
for i in nums:
if i not in hash_table:
hash_table[i]=1
else:
hash_table[i] += 1
if hash_table[i] >=3:
del hash_table[i]
return hash_table.keys()[0]



class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return (2*sum(set(nums)) - sum(nums))


#137. Single Number II
class Solution(object):
    def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hash_table={}
for i in nums:
if i not in hash_table:
hash_table[i]=1
else:
hash_table[i] += 1
if hash_table[i] >=3:
del hash_table[i]
return hash_table.keys()[0]

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return (3*sum(set(nums)) - sum(nums))/2
 
260. Single Number III
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
diff = 2*sum(set(nums)) - sum(nums)
for i in nums:
j=diff - i
if j in nums:
return [j,i]
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hash_table={}
for i in nums:
if i not in hash_table:
hash_table[i]=1
else:
hash_table[i] += 1
if hash_table[i] ==2:
del hash_table[i]
return hash_table.keys()


287. Find the Duplicate Number
class Solution:
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
hash_table ={}
for i in nums:
if i not in hash_table:
hash_table[i]=1
else:
hash_table[i]+=1
if hash_table[i] >1:
return i
 
posted @ 2018-06-28 02:23  ffeng0312  阅读(131)  评论(0)    收藏  举报