1. 程式人生 > >劍指Offer-56 陣列中只出現一次的兩個數字

劍指Offer-56 陣列中只出現一次的兩個數字

題目:

一個整型數組裡除了兩個數字之外,其他的數字都出現了兩次。請寫程式找出這兩個只出現一次的數字。你可以假設這兩個數字一定存在。
樣例
輸入:[1,2,3,3,4,4]
輸出:[1,2]

解答:

class Solution(object):
	def findNumsAppearOnce(self, nums):
		"""
		:type nums: List[int]
		:rtype: List[int]
		"""
		xor = 0
		for i in nums:
			xor = xor ^ i
		count = 0
		while(xor != 0):
			r =
xor & 0b1 count += 1 if r == 1: break else: xor = xor >> 1 count -= 1 split = 1 while(count > 0): split = split << 1 count -= 1 first, second = 0, 0 for i in nums: if i & split == 0: first = first ^ i else: second = second ^ i return
[first, second]