0%

数组中只出现一次的数字

题目描述

一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。

解答

使用HashMap的话空间复杂度为O(N),这里使用异或抵消两个相同的数,空间复杂度可降为O(1)

时间复杂度都为O(N)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void FindNumsAppearOnce(int[] array, int num1[], int num2[]) {
int nor = 0;
for (int i : array) {
nor = nor ^ i;
}
int lastN = 0;//nor中1出现的最低位
for (; (nor & 1) == 0; nor = nor >> 1) {
lastN++;
}
/** 将array分为两个子数组分别异或 */
int ans1 = 0, ans2 = 0;
for (int i : array) {
if (((i >> lastN) & 1) == 0) {
ans1 ^= i;
} else {
ans2 ^= i;
}
}
num1[0] = ans1;
num2[0] = ans2;
}

网上题解

链接:https://www.nowcoder.com/questionTerminal/e02fdb54d7524710a7d664d082bb7811?answerType=1&f=discussion
来源:牛客网

简单方法

可以先用最简单的HashMap的方法来做,这样主要是为了练习Map的用法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.HashMap;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
//哈希算法
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0; i < array.length; i++){
if(map.containsKey(array[i]))
map.put(array[i],2);
else
map.put(array[i],1);
}
int count = 0;
for(int i=0; i < array.length; i++){
if(map.get(array[i]) == 1){
if(count == 0){
num1[0] = array[i];
count++;
}else
num2[0] = array[i];
}
}

}
}

比较考验智商的做法

比较好用的剑指offer的做法:
首先:位运算中异或的性质:两个相同数字异或=0,一个数和0异或还是它本身。
当只有一个数出现一次时,我们把数组中所有的数,依次异或运算,最后剩下的就是落单的数,因为成对儿出现的都抵消了。

依照这个思路,我们来看两个数(我们假设是AB)出现一次的数组。我们首先还是先异或,剩下的数字肯定是A、B异或的结果,这个结果的二进制中的1,表现的是A和B的不同的位。我们就取第一个1所在的位数,假设是第3位,接着把原数组分成两组,分组标准是第3位是否为1。如此,相同的数肯定在一个组,因为相同数字所有位都相同,而不同的数,肯定不在一组。然后把这两个组按照最开始的思路,依次异或,剩余的两个结果就是这两个只出现一次的数字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {

int xor1 = 0;
for(int i=0; i < array.length; i++)
xor1 = xor1^array[i];
//在xor1中找到第一个不同的位对数据进行分类,分类为两个队列对数据进行异或求和找到我们想要的结果
int index = 1;
while((index & xor1)==0)
index = index <<1;//因为可能有多个位为1所以需要求一下位置
int result1 = 0;
int result2 = 0;
for(int i=0; i < array.length; i++){
if((index & array[i]) == 0)
result1 = result1^array[i];
else
result2 = result2^array[i];
}
num1[0] = result1;
num2[0] = result2;
}