Skip to content

Commit 593a89b

Browse files
committed
solve problem Find All Duplicates In An Array
1 parent 2a63b85 commit 593a89b

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ All solutions will be accepted!
241241
|284|[Peeking Iterator](https://leetcode-cn.com/problems/peeking-iterator/description/)|[java/py](./algorithms/PeekingIterator)|Medium|
242242
|328|[Odd Even Linked List](https://leetcode-cn.com/problems/odd-even-linked-list/description/)|[java/py/js](./algorithms/OddEvenLinkedList)|Medium|
243243
|677|[Map Sum Pairs](https://leetcode-cn.com/problems/map-sum-pairs/description/)|[java/py/js](./algorithms/MapSumPairs)|Medium|
244+
|442|[Find All Duplicates In An Array](https://leetcode-cn.com/problems/find-all-duplicates-in-an-array/description/)|[java/py/js](./algorithms/FindAllDuplicatesInAnArray)|Medium|
244245

245246
# Database
246247
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Find All Duplicates In An Array
2+
This problem is easy to solve
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public List<Integer> findDuplicates(int[] nums) {
3+
List<Integer> res = new ArrayList<Integer>();
4+
5+
for (int num : nums) {
6+
num = Math.abs(num);
7+
if (nums[num - 1] < 0) {
8+
res.add(num);
9+
} else {
10+
nums[num - 1] *= -1;
11+
}
12+
}
13+
return res;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var findDuplicates = function(nums) {
6+
let res = []
7+
8+
nums.forEach(num => {
9+
num = Math.abs(num)
10+
if (nums[num - 1] < 0) {
11+
res.push(num)
12+
} else {
13+
nums[num - 1] *= -1
14+
}
15+
})
16+
17+
return res
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def findDuplicates(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
res = []
8+
for num in nums:
9+
num = abs(num)
10+
if nums[num - 1] < 0:
11+
res.append(num)
12+
else:
13+
nums[num - 1] *= -1
14+
15+
return res

0 commit comments

Comments
 (0)