题目链接:https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array/
思路一:引入Hashmap,遍历数组,将元素存入Hashmap,接下来遍历[1,n],找出不存在于Hashmap的元素,追加到结果数组中。代码如下:
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
if(nums == null) return null;
//遍历nums将元素添加到Hashmap
HashMap<Integer,Integer> curNums = new HashMap<Integer,Integer>();
for(int num:nums) {
if(curNums.get(num) == null) {
curNums.put(num,1);
}
}
List<Integer> result = new ArrayList<Integer>();
//检索HashMap找到不存在的数字
for(int i&

本文记录了LeetCode第448题的解决方案,通过使用HashMap或避免额外存储空间,来查找[1, n]区间内不在数组中的数字。两种方法的时间复杂度均为O(n),但空间复杂度分别为O(n)和O(1)。"
108158651,8765294,GreenSock动画平台初学者指南,"['JavaScript', '前端框架', '动画库', '图形设计']
最低0.47元/天 解锁文章
328

被折叠的 条评论
为什么被折叠?



