Given an array of integers, every element appears three times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
length = len(nums)
n = length / 3 + 1
#ans = nums[0]
for i in range(n):
if 3*i == length-1:
return nums[3*i]
elif nums[3*i] == nums[3*i+1] and nums[3*i] == nums[3*i+2]:
continue
else:
return nums[3*i]