题解 | #数组中只出现一次的两个数字#
数组中只出现一次的两个数字
https://www.nowcoder.com/practice/389fc1c3d3be4479a154f63f495abff8
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums int整型一维数组
* @return int整型一维数组
*/
public int[] FindNumsAppearOnce (int[] nums) {
// write code here
int []rst = new int[2];
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
if (map.containsKey(nums[i])) {
map.remove(nums[i]);
} else {
map.put(nums[i], 1);
}
}
int count = 0;
for (int i: map.keySet()) {
rst[count++] = i;
}
return rst;
}
}
用hashmap或者hashset都可以的;
如果在set、map出现, 就移除该元素; 如果没有出现,就添加该元素;
最后将set、map中的元素放入数组里面
note_coding 文章被收录于专栏
记录自己的解题思路, 欢迎评价
智元机器人成长空间 174人发布