448. 找到所有数组中消失的数字
难度简单462收藏分享切换为英文关注反馈
给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。
找到所有在 [1, n] 范围之间没有出现在数组中的数字。
您能在不使用额外空间且时间复杂度为*O(n)*的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。
示例:
输入:
[4,3,2,7,8,2,3,1]
输出:
[5,6]
通过次数55,420
提交次数92,080
题解
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
HashMap<Integer,Integer> m = new HashMap<>();
List<Integer> res = new ArrayList<>();
for(int i : nums){
m.put( i,0);
}
for(int i = 1 ; i < nums.length+1 ;i++){
if(! m.containsKey(i)){
res.add(i);
}
}
return res;
}
}
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 mym_74@163.com