-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindDuplicates.py
39 lines (34 loc) · 945 Bytes
/
findDuplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
'''
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
Find all the elements that appear twice in this array.
Could you do it without extra space and in O(n) runtime?
Example:
Input:
[4,3,2,7,8,2,3,1]
Output:
[2,3]
'''
"""
:type nums: List[int]
:rtype: List[int]
"""
class Solution(object):
def findDuplicates(self, nums):
i = 0
ans = []
while i < len(nums):
if nums[i] != i+1 and nums[i] != nums[nums[i]-1]:
tmp = nums[i]
nums[i] = nums[nums[i]-1]
nums[tmp-1] = tmp
else:
i += 1
for i in range(len(nums)):
if nums[i] != i+1:
ans.append(nums[i])
return sorted(ans)
if __name__ == "__main__":
sol = Solution()
nums = [2,2]
print(sol.findDuplicates(nums))