一、题目描述
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
二、思路分析及代码实现
方法一:位运算(书中推荐方法)
1 、遍历数组,将所有的数组元素异或结果为A、B两个元素的异或结果(不妨假设只出现一次的数字分别为A和B)
2、找到异或结果的二进制形式的第一位为1的位数J
3、遍历数组,根据第J为是否为1分成两组
4、对两组分别进行异或,则可以得到只出现一次的数字。
求整数中1的个数
代码实现:
//num1,num2分别为长度为1的数组。传出参数
//将num1[0],num2[0]设置为返回结果
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
int res1=0;
for(int i=0;i<array.length;i++){
res1=res1^array[i];
}
//从右往左依次设置当前一位为1,其余为0,若结果为0,则表示当前位一定为0
int index=1;
while((res1&index)==0){
index=index<<1;
}
num1[0]=0;
num2[0]=0;
//根据index确定为1的位数为哪一位,并分组
for(int i=0;i<array.length;i++){
if((array[i]&index)==0){
num1[0]=num1[0]^array[i];
}else{
num2[0]=num2[0]^array[i];
}
}
}
}
方法二:数组和栈实现
1、数组排序 Arrays.sort()
2、遍历排序后的数组,若栈为空,则入栈
3、若栈不空,执行如下草作:
3.1若栈顶元素==当前数组元素,则出栈;
3.2若栈顶元素!=当前数组元素,则入栈;
4、遍历完数组之后,栈中元素即为所求的只出现一次的元素。
代码实现:
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
Arrays.sort(array);
Stack<Integer> stack=new Stack<>();
for(int i=0;i<array.length;i++){
if((!stack.isEmpty())&&stack.peek()==array[i]){
stack.pop();
}else{
stack.push(array[i]);
}
}
num1[0]=stack.pop();
num2[0]=stack.pop();
}
}
方法三:使用ArrayList的contain方法
1、如果链表中包含当前元素,则删除
删除时需要注意一下,如果直接传入当前数作为参数,它会按照下标进行删除,不会按照对象进行删除,可能会出现越界。
2、如果当前链表不包含当前元素,则添加当前元素进链表。
代码实现:
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
if(array.length < 2){
return;
}
ArrayList<Integer> list = new ArrayList();
for(int i = 0;i<array.length;i++){
if(list.contains(array[i])){
list.remove(new Integer(array[i]));
}else{
list.add(array[i]);
}
}
num1[0] = list.get(0);
num2[0] = list.get(1);
}
}
方法四:HashMap实现
1、遍历数组,将所有元素及出现次数放在HashMap中;
2、遍历HashMap,输出只出现一次的元素。
可优化:
若检测到当前key已出现,则删除当前key。
HashMap和ArrayList有很多相似的地方:
- 通过Key查询值:get
- 通过put()来插入value
- ContainsKey()则用来检验对象是否已经存在
代码实现:
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
HashMap<Integer, Integer> map=new HashMap<>();
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];
break;
}
}
}
}
}
优化后的代码:
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
HashMap<Integer, Integer> map=new HashMap<>();
for(int i=0;i<array.length;i++){
if(map.containsKey(array[i])){
map.remove(array[i]);
}else{
map.put(array[i],1);
}
}
int count=0;
for(int i=0;i<array.length;i++){
if(map.containsKey(array[i])){
if(count==0){
num1[0]=array[i];
count++;
}else{
num2[0]=array[i];
break;
}
}
}
}
}
方法五:通过HashSet实现
思路同上,只是采用的数据结构不同。
代码实现:
import java.util.*;
public class Solution {
public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) {
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < array.length; i++) {
if (!set.contains(array[i])) set.add(array[i]);
else set.remove(array[i]);
}
int k = 0;
for (Integer i : set) {
if(k++ == 0) num1[0] = i;
else num2[0] = i;
}
}
}