LeetCode448 — Find All Numbers Disappeared in an Array
程序员文章站
2022-07-15 13:07:27
...
题目:在一个数组找出未出现的数字
思路:经过前几题的洗礼,一看到n个数中有1-n个数就应该能知道用索引对应数值的方法区解题了
遍历一遍,如果第一次能到的话,则将其负,这里不管重复,题目只要求输出没有的值,那么遍历一遍之后还有正数的话,说明这个点的索引到不了,没有这个值能到这个索引。
public class Solution448 {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> res = new ArrayList<>();
for(int i = 0; i < nums.length; i ++){
int index = Math.abs(nums[i]) - 1;
if(nums[index] > 0) nums[index] = -nums[index];
}
for(int i = 0; i < nums.length; i ++){
if(nums[i] > 0)
res.add(i+1);
}
return res;
}
}
上一篇: Java之异常处理具体使用总结
推荐阅读
-
LeetCode448 — Find All Numbers Disappeared in an Array
-
LeetCode442 — Find All Duplicates in an Array
-
leetcode 448. Find All Numbers Disappeared in an Array
-
LeetCode-448. Find All Numbers Disappeared in an Array
-
Leetcode 448. Find All Numbers Disappeared in an Array (python+cpp)
-
LeetCode之Find All Duplicates in an Array
-
Find All Duplicates in an Array
-
Find All Duplicates in an Array
-
【LeetCode】442. Find All Duplicates in an Array 找出数组中所有重复项