-
[LeetCode] 442. Find All Duplicates in an ArrayAlgorithm/LeetCode 2021. 9. 25. 09:21
Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appears twice.
You must write an algorithm that runs in O(n) time and uses only constant extra space.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1] Output: [2,3]
Example 2:
Input: nums = [1,1,2] Output: [1]
Example 3:
Input: nums = [1] Output: []
Constraints:
- n == nums.length
- 1 <= n <= 105
- 1 <= nums[i] <= n
- Each element in nums appears once or twice.
### Solution class Solution: def findDuplicates(self, nums: List[int]) -> List[int]: ze = [0 for i in range(len(nums))] C = {k : v for k,v in zip(nums, ze)} for e in nums: C[e] += 1 stack = [] for k,v in C.items(): if v >= 2: stack.append(k) return stack
#memo
value 값이 2개 이상인 key 들만 찾아서 return 해주는 문제.
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode]496. Next Greater Element I (0) 2021.09.26 [LeetCode] 1935. Maximum Number of Words You Can Type (0) 2021.09.25 [LeetCode] 1460. Make Two Arrays Equal by Reversing Sub-arrays (0) 2021.09.25 [LeetCode] 1470. Shuffle the Array (0) 2021.09.24 [Leetcode] 1742. Maximum Number of Balls in a Box (0) 2021.09.23