LeetCode442 — Find All Duplicates in an Array
程序员文章站
2022-07-15 13:10:27
...
题目:和287类似,只不过这次出现重复的数字不只是一个,是多个。
思路:如果可以开辟空间的话很简单,不能开辟空间的话,稍微思考一下还是有思路的。
和287不同的是,这道题没有规定数组不能够修改,那么就可以访问这个数字为标记一下是否访问过。
public class Solution442 {
public List<Integer> findDuplicates(int[] nums) {
List<Integer> res = new ArrayList<>();
for(int i = 0; i < nums.length; i ++){
int index = Math.abs(nums[i]);
if(nums[index-1] > 0) nums[index-1] = - nums[index-1];
else res.add(Math.abs(nums[i]));
}
return res;
}
}
推荐阅读
-
leetcode 442. Find All Duplicates in an Array
-
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 找出数组中所有重复项