一个整型数组里除了两个数字只出现一次,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
数据范围:数组长度
,数组中每个数的大小 
要求:空间复杂度
,时间复杂度 )
要求:空间复杂度
提示:输出时按非降序排列。
function FindNumsAppearOnce( array ) {
// write code here
var res = 0; //对整个数组求异或的结果
for(var i = 0; i < array.length; i++){
res ^= array[i];
}
var compare = 1;
while((compare & res) == 0){ //判断异或结果的二进制第一位是否为1,为1则直接跳过该循环
compare <<= 1; //为0则继续往后找,一直到找到为1的二进制位,该行代码也相当于compare *=2
}
var a = 0;
var b = 0;
for(var i = 0; i < array.length; i++){ //遍历数组,开始判断数字们的compare位是否为1
if((compare & array[i]) == 0){ //如果该数字二进制的第某位为0,则分到数组一
a ^= array[i]; //对数组一进行异或,得到a
}else{ //如果该数字二进制的第某位为1,则分到数组二
b ^= array[i]; //对数组二进行异或,得到b
}
}
return a < b ? [a,b] : [b,a];
} class Solution: def FindNumsAppearOnce(self , array ): # write code here if not array: return [] res = 0 for i in array: res = res ^ i index = 0 while res & 1 == 0: res = res >> 1 index += 1 a, b = 0, 0 for i in array: if self.helper(i, index): a = a ^ i else: b = b ^ i return [a, b] if a < b else [b, a] def helper(self, num, index): num = num >> index return num & 1 != 0
class Solution {
public:
vector<int> FindNumsAppearOnce(vector<int>& array) {
// write code here
unordered_map<int,int> map;
vector<int> res;
for(auto it:array){
if(map.find(it)!=map.end()) map.erase(it);//若已经出现过一次,删掉
else map[it]++;}//若第一次出现,保留
for(auto it:map)//此时map里只剩两个数字,取出来,存到vector类型的res里边
res.push_back(it.first);
sort(res.begin(),res.end());//从小到大排序
return res;}}; import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] nums) {
// write code here
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
if (set.contains(num)) {
set.remove(num);
} else {
set.add(num);
}
}
return set.stream().sorted().mapToInt(Integer::intValue).toArray();
}
} class Solution:
# 异或运算:相同数字为0,相异数字为1
# 空间复杂度:O(1) 时间复杂度:O(n)
def FindNumsAppearOnce(self , array: List[int]) -> List[int]:
res = [0, 0]
temp = 0
for i in range(len(array)):
temp ^= array[i] # 0 ^ 数 = 数
k = 1
# 找到两个数不相同的第一位
while k & temp == 0:
k <<= 1 # 左移,右边补零
for i in range(len(array)):
# 遍历数组,对每个数分类
if k & array[i] == 0:
res[0] ^= array[i]
else:
res[1] ^= array[i]
res.sort()
return resclass Solution:
# 哈希表
# 空间复杂度:O(n) 时间复杂度:O(n)
def FindNumsAppearOnce(self , array: List[int]) -> List[int]:
mp = {}
res = []
for i in array:
if i in mp:
mp[i] += 1
else:
mp[i] = 1
for i in mp:
if mp[i] == 1:
res.append(i)
res.sort()
return res
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param array int整型一维数组 # @return int整型一维数组 # class Solution: def FindNumsAppearOnce(self , array ): import collections array_count = collections.Counter(array) return [key for key,value in array_count.items() if value==1] # write code here
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型vector
* @return int整型vector
*/
vector<int> FindNumsAppearOnce(vector<int>& array) {
// write code here
int temp=0;
for(int i=0;i<array.size();i++){
temp^=array[i];
}
int ll=1;
while(1){
if(temp&ll)break;
ll<<=1;
}
int a=0,b=0;
for(int i=0;i<array.size();i++){
if(ll&array[i])a^=array[i];
else b^=array[i];
}
if(a<=b){
return {a,b};
}else{
return {b,a};
}
}
}; public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] array) {
// write code here
int[] res = new int[2];
Arrays.sort(array);
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
map.put(array[i], map.get(array[i]) + 1);
} else {
map.put(array[i], 1);
}
}
int k = 0;
for (int i = 0; i < array.length; i++) {
if (map.get(array[i]) == 1) {
res[k] = array[i];
k++;
}
}
return res;
}
} import java.util.*;
public class Solution {
public int[] FindNumsAppearOnce (int[] array) {
// 使用HashMap统计数值出现的次数
// key -- 数值,value -- 该数值出现的次数
Map<Integer, Integer> red = new HashMap<>();
// ans用来保存结果值
int[] ans = new int[2];
// index用来控制ans的索引,指向当前数值存储的位置
int index = 0;
// 扫描一次数组,进行统计
for(int num : array)
red.put(num, red.getOrDefault(num, 0) + 1);
// 扫描一次HashMap,寻找之出现一次的数值并记录
// 当index等于2,说明已经找到了这两个数值,不必继续扫表
for(Map.Entry<Integer, Integer> entry : red.entrySet()){
if(entry.getValue() == 1)
ans[index ++] = entry.getKey();
if(index == 2)
break;
}
// 结果需要非降序排列,因此这里做了个排序
Arrays.sort(ans);
return ans;
}
} public int[] FindNumsAppearOnce (int[] array) {
int[] res = new int[2];
if(array == null || array.length == 0) return res;
//0和谁异或(^)都是该数本身
int x = 0;
for(int i = 0;i < array.length;i++){
x ^= array[i];
}
int m = 1;
//想要的是m左移后的那个数
while((x & 1) != 1){
x >>= 1;
m <<= 1;
}
res[0] = res[1] = 0;
//左移后得到的m可以把数组分为2组
for(int i = 0;i < array.length;i++){
//但是m和array[i]与运算结果是m本身或0
if((array[i] & m) == m){
res[0] ^= array[i];
}else{
res[1] ^= array[i];
}
}
//把大的放到res[1]
if(res[0] > res[1]){
int temp = res[0];
res[0] = res[1];
res[1] = temp;
}
return res;
} public class Solution {
public int[] FindNumsAppearOnce (int[] array) {
// 用以提取异或结果
Function<IntPredicate, Integer> getXor = (intPredicate) -> Arrays.stream(array)
.filter(intPredicate).reduce((a, b) -> a ^ b).getAsInt();
// 提取全量数据的异或结果
int xorResult = getXor.apply(a -> true);
// 取全量异或结果的末位
int lastBit = xorResult ^ (xorResult & (xorResult - 1));
int[] result = new int[2];
// 按数据与全量异或结果的末位是否相等,进行二次异或
result[0] = getXor.apply(a -> (a & lastBit) == lastBit);
result[1] = getXor.apply(a -> (a & lastBit) == 0);
Arrays.sort(result);
return result;
}
} class Solution: def FindNumsAppearOnce(self , nums: List[int]) -> List[int]: # write code here result = dict() for num in nums: if num in result: result.pop(num) else: result[num] = 1 return sorted(result.keys())
int* FindNumsAppearOnce(int* nums, int numsLen, int* returnSize ) {
int HashTable[1000000] = {0}, i, j, *res;
res = (int*)malloc(2*sizeof(int));
for (i = 0; i < numsLen; i++)
HashTable[nums[i]]++;
for (i = 0, j = 0; i < sizeof(HashTable); i++) {
if (HashTable[i] == 1)
res[j++] = i;
if(j>=2)
break;
}
*returnSize = 2;
return res;
}