题解 | 数组中只出现一次的两个数字
数组中只出现一次的两个数字
https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] nums) {
if(nums.length==0){
return new int[0];
}
// write code here
HashMap <Integer,Integer> hashMap = new HashMap<>();
for(int num:nums){
if(!hashMap.containsKey(num)){
hashMap.put(num,1);
}else{
int temp = hashMap.get(num) +1;
hashMap.put(num,temp);
}
}
List<Integer> res = new ArrayList<>();
for(Map.Entry<Integer,Integer> temp :hashMap.entrySet()){
if(temp.getValue()==1){
res.add(temp.getKey());
}
}
// 不能初始化为空
int []array = new int[res.size()];
for (int i = 0;i<res.size();i++){
array[i] = res.get(i);
}
return array;
}
}
小天才公司福利 1370人发布
查看10道真题和解析