题解 | 数组中只出现一次的两个数字
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] nums) {
// write code here
Map<Integer, Integer> map = new HashMap<>();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
if (map.containsKey(nums[i])) {
set.remove(nums[i]);
}
map.put(nums[i], i);
}
return set.stream().mapToInt(Integer::intValue).toArray();
}
}
查看22道真题和解析