落单的数
给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字。
样例
给出 [1,2,2,1,3,4,3],返回 4
挑战
一次遍历,常数级的额外空间复杂度
解题思路:
最快的是利用
collections.Counter() python自带的函数,其次是利用hash表,不过利用异或的思路挺新奇的。
思路1:利用异或的性质(自反性)
自反性:a ^ b ^ a = b.
异或运算最常见于多项式除法,不过它最重要的性质还是自反性:
A^B^ B=A,即对给定的数A,用同样的运算因子(B)作两次异或运算后仍得到A本身。这是一个神奇
的性质,利用这个性质,可以获得许多有趣的应用。例如,所有的程序教科书都会向初学者指出,要交换两个变量的值,必须要引入一个中间变量。但如果使用异或,就可以节约一个变量的存储空间:
设有A,B两个变量,存储的值分别为a,b,则以下三行表达式将互换他们的值 表达式(值):
A=A^B(a^b)
B=B^A(b^a^b=a)
A=A^B(a^b^a=b)
这道题利用了异或位运算的一个性质,即:一个数与自身异或的结果为0。我们只需遍历数组中的每一个元素,并将其进行异或。因为,异或满足交换律,所以最终的异或结果将仅仅包含只出现一次的那个数。
如:1 ^ 2 ^ 2 ^ 1 ^3 ^ 4 ^ 3 = 1 ^ 1 ^ 2 ^ 2 ^ 3 ^ 3 ^ 4 = 4
代码如下:
class Solution:
"""
@param A: An integer array
@return: An integer
"""
'''
def singleNumber(self, A):
# write your code here
hashmap = {}
for num in A:
hashmap[num] = hashmap.get(num, 0) + 1
min_key = min(hashmap, key = lambda x : hashmap[x])
return min_key
'''
#利用异或运算
def singleNumber(self, A):
# write your code here
ans = 0
for i in A:
ans = ans ^ i
return ans
时间复杂度O(n),空间复杂度O(1)
思路二:
若一个数字出现2次,则该数字的二进制表示中每个位置的数值为1的出现次数为2次,如:5的二进制表示为101,若5出现3次,则其二进制表示中的第一个位置和第二个位置出现1的次数和均为2。那么对该位1出现的次数对2取余,最终的结果所对应的就是所要求的Sing Number。
代码:
def singleNumber(self, A):
if len(A) < 2:
return A[0]
res = 0
for i in range(32):
count = 0
for j in range(len(A)):
if self.isBit1(A[j], i):
count += 1
res |= (count % 2) << i
return res
def isBit1(self, num, index):
num = num >> index
return (num&1)
时间复杂度O(n),空间复杂度O(1)
落单的数 II
给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字。
样例
给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4
挑战
一次遍历,常数级的额外空间复杂度
思路:
与Single Number I 类似,所不同的是这道题除了一个数字之外都出现3次,仍然考虑使用位操作,尝试消除掉出现3次的数字。
若一个数字出现3次,则该数字的二进制表示中每个位置的数值为1的出现次数为3次,如:5的二进制表示为101,若5出现3次,则其二进制表示中的第一个位置和第二个位置出现1的次数和均为3。那么对该位1出现的次数对3取余,最终的结果所对应的就是所要求的Sing Number。
解题方法:与Single Number I 类似,所不同的是这道题除了一个数字之外都出现3次,仍然考虑使用位操作,尝试消除掉出现3次的数字。
若一个数字出现3次,则该数字的二进制表示中每个位置的数值为1的出现次数为3次,如:5的二进制表示为101,若5出现3次,则其二进制表示中的第一个位置和第二个位置出现1的次数和均为3。那么对该位1出现的次数对3取余,最终的结果所对应的就是所要求的Sing Number。
代码如下:
class Solution:
"""
@param A: An integer array
@return: An integer
"""
'''
def singleNumberII(self, A):
# write your code here
n = len(A)
res = 0
for i in range(32):
count = 0
for j in range(n):
if self.isBit1(A[j], i):
count += 1
res |= (count % 3) << i
return res
def isBit1(self, num, index):
num = num >> index
return (num&1)
'''
def singleNumberII(self, A):
# write your code here
for val, cnt in collections.Counter(A).items():
if cnt == 1:
return val
时间复杂度O(n),空间复杂度O(1)
落单的数 III
给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字。
样例
给出 [1,2,2,3,4,4,5,3],返回 1和5
挑战
O(n)时间复杂度,O(1)的额外空间复杂度
思路:
与以上两题不同的是,这道题有两个数只出现一次。基本的思路还是利用位运算,除去出现次数为2次的数。
如果对所有元素进行异或操作,最后剩余的结果是出现次数为1次的两个数的异或结果,此时无法直接得到这两个数具体的值。但是,因为这两个数一定是不同的,所以最终异或的值至少有一个位为1。我们可以找出异或结果中第一个值为1的位,然后根据该位的值是否为1,将数组中的每一个数,分成两个部分。这样每个部分,就可以采用Sing number I中的方法得到只出现一次的数。
代码:
class Solution:
"""
@param A: An integer array
@return: An integer array
"""
'''
def singleNumberIII(self, A):
# write your code here
res = []
for val, cnt in collections.Counter(A).items():
if cnt == 1:
res.append(val)
return res
'''
'''
def singleNumberIII(self, A):
ret = []
a, b, xor, loc = 0, 0, 0, 0
for i in A:
xor ^= i
for i in range(32):
if (xor>>i)&1 == 1:loc = i
for i in A:
ks = (i >> loc)&1
if ks == 1: a ^= i
else:
b ^= i
ret.append(a)
ret.append(b)
return ret
'''
def singleNumberIII(self, A):
res = []
twonumbers = 0
for i in range(len(A)):
twonumbers ^= A[i]
index = self.findindex(twonumbers)
num1, num2 = 0, 0
for i in range(len(A)):
if self.isBit1(A[i], index):
num1 ^= A[i]
else:
num2 ^= A[i]
res.append(num1)
res.append(num2)
return res
def isBit1(self, num, index):
num = num >> index
return (num&1)
def findindex(self, number):
index = 0
while (number&1) == 0:
index += 1
number >>= 1
return index