数组中只出现一次的数字 python_剑指Offer(四十):数组中只出现一次的数字

摘要

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。

一、前言

本系列文章为《剑指Offer》刷题笔记。

刷题平台:牛客网

书籍下载:共享资源

二、题目

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。

1、思路

大家首先想到的是顺序扫描法,但是这种方法的时间复杂度是O(n^2)。接着大家又会考虑用哈希表的方法,但是空间复杂度不是O(1)。

应该怎么做才能即满足时间复杂度是O(n)又满足空间复杂度是O(1)的要求呢?

我们可以想一想“异或”运算的一个性质,我们直接举例说明。

举例:{2,4,3,6,3,2,5,5}

这个数组中只出现一次的两个数分别是4和6。怎么找到这个两个数字呢?

我们先不看找到俩个的情况,先看这样一个问题,如何在一个数组中找到一个只出现一次的数字呢?比如数组:{4,5,5},唯一一个只出现一次的数字是4。

我们知道异或的一个性质是:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现一次的数字。比如数组{4,5,5},我们先用数组中的第一个元素4(二进制形式:0100)和数组中的第二个元素5(二进制形式:0101)进行异或操作,0100和0101异或得到0001,用这个得到的元素与数组中的三个元素5(二进制形式:0101)进行异或操作,0001和0101异或得到0100,正好是结果数字4。这是因为数组中相同的元素异或是为0的,因此就只剩下那个不成对的孤苦伶仃元素。

现在好了,我们已经知道了如何找到一个数组中找到一个只出现一次的数字,那么我们如何在一个数组中找到两个只出现一次的数字呢?如果,我们可以将原始数组分成两个子数组,使得每个子数组包含一个只出现一次的数字,而其他数字都成对出现。这样,我们就可以用上述方法找到那个孤苦伶仃的元素。

我们还是从头到尾一次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数组的异或结果。因为其他数字都出现了两次,在异或中全部抵消了。由于两个数字肯定不一样,那么异或的结果肯定不为0,也就是说这个结果数组的二进制表示至少有一个位为1。我们在结果数组中找到第一个为1的位的位置,记为第n位。现在我们以第n位是不是1为标准把元数组中的数字分成两个子数组,第一个子数组中每个数字的第n位都是1,而第二个子数组中每个数字的第n位都是0。

举例:{2,4,3,6,3,2,5,5}

我们依次对数组中的每个数字做异或运行之后,得到的结果用二进制表示是0010。异或得到结果中的倒数第二位是1,于是我们根据数字的倒数第二位是不是1分为两个子数组。第一个子数组{2,3,6,3,2}中所有数字的倒数第二位都是1,而第二个子数组{4,5,5}中所有数字的倒数第二位都是0。接下来只要分别两个子数组求异或,就能找到第一个子数组中只出现一次的数字是6,而第二个子数组中只出现一次的数字是4。

2、代码

C++:

C++

class Solution {

public:

void FindNumsAppearOnce(vector data,int* num1,int *num2) {

int length = data.size();

if(length < 2){

return;

}

// 对原始数组每个元素求异或

int resultExclusiveOR = 0;

for(int i = 0; i < length; ++i){

resultExclusiveOR ^= data[i];

}

unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR);

*num1 = *num2 = 0;

for(int j = 0; j < length; j++){

if(IsBit1(data[j], indexOf1)){

*num1 ^= data[j];

}

else{

*num2 ^= data[j];

}

}

}

private:

// 找到二进制数num第一个为1的位数,比如0010,第一个为1的位数是2。

unsigned int FindFirstBitIs1(int num){

unsigned int indexBit = 0;

// 只判断一个字节的

while((num & 1) == 0 && (indexBit < 8 * sizeof(unsigned int))){

num = num >> 1;

indexBit++;

}

return indexBit;

}

// 判断第indexBit位是否为1

bool IsBit1(int num, unsigned int indexBit){

num = num >> indexBit;

return (num & 1);

}

};

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43classSolution{

public:

voidFindNumsAppearOnce(vectordata,int*num1,int*num2){

intlength=data.size();

if(length<2){

return;

}

// 对原始数组每个元素求异或

intresultExclusiveOR=0;

for(inti=0;i

resultExclusiveOR^=data[i];

}

unsignedintindexOf1=FindFirstBitIs1(resultExclusiveOR);

*num1=*num2=0;

for(intj=0;j

if(IsBit1(data[j],indexOf1)){

*num1^=data[j];

}

else{

*num2^=data[j];

}

}

}

private:

// 找到二进制数num第一个为1的位数,比如0010,第一个为1的位数是2。

unsignedintFindFirstBitIs1(intnum){

unsignedintindexBit=0;

// 只判断一个字节的

while((num&1)==0&&(indexBit<8*sizeof(unsignedint))){

num=num>>1;

indexBit++;

}

returnindexBit;

}

// 判断第indexBit位是否为1

boolIsBit1(intnum,unsignedintindexBit){

num=num>>indexBit;

return(num&1);

}

};

Python:

Python

# -*- coding:utf-8 -*-

class Solution:

# 返回[a,b] 其中ab是出现一次的两个数字

def FindNumsAppearOnce(self, array):

# write code here

if len(array) <= 0:

return []

resultExclusiveOR = 0

length = len(array)

for i in array:

resultExclusiveOR ^= i

firstBitIs1 = self.FindFisrtBitIs1(resultExclusiveOR)

num1, num2 = 0, 0

for i in array:

if self.BitIs1(i, firstBitIs1):

num1 ^= i

else:

num2 ^= i

return num1, num2

def FindFisrtBitIs1(self, num):

indexBit = 0

while num & 1 == 0 and indexBit <= 32:

indexBit += 1

num = num >> 1

return indexBit

def BitIs1(self, num, indexBit):

num = num >> indexBit

return num & 1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30# -*- coding:utf-8 -*-

classSolution:

# 返回[a,b] 其中ab是出现一次的两个数字

defFindNumsAppearOnce(self,array):

# write code here

iflen(array)<=0:

return[]

resultExclusiveOR=0

length=len(array)

foriinarray:

resultExclusiveOR^=i

firstBitIs1=self.FindFisrtBitIs1(resultExclusiveOR)

num1,num2=0,0

foriinarray:

ifself.BitIs1(i,firstBitIs1):

num1^=i

else:

num2^=i

returnnum1,num2

defFindFisrtBitIs1(self,num):

indexBit=0

whilenum&1==0andindexBit<=32:

indexBit+=1

num=num>>1

returnindexBit

defBitIs1(self,num,indexBit):

num=num>>indexBit

returnnum&1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值