==========================努力奋斗财源广进==========================
一个整型数组里除了两个数字只出现一次,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。 数据范围:数组长度2≤n≤1000,数组中每个数的大小0<val≤1000000 要求:空间复杂度 O(1),时间复杂度 O(n)
提示:输出时按非降序排列。
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] array) {
// write code here
int[] res = new int[2];
HashMap<Integer, Object> set = new HashMap<>();
int length = array.length;
for(int i = 0; i < length; i++)
{
if(set.containsKey(array[i]))
{
set.remove(array[i],null);
}
else
{
set.put(array[i],null);
}
}
int i = 0;
for(Integer num:set.keySet())
{
res[i++] = num;
}
return res;
}
}
//待定
评论区记录复习记录
评论