-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ep9_findDuplicate.py
61 lines (45 loc) · 1.3 KB
/
Ep9_findDuplicate.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
Leetcode #287. Find the Duplicate Number (https://leetcode.com/problems/find-the-duplicate-number/)
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
There is only one repeated number in nums, return this repeated number.
Example 1:
Input: nums = [1,3,4,2,2]
Output: 2
Example 2:
Input: nums = [3,1,3,4,2]
Output: 3
Example 3:
Input: nums = [1,1]
Output: 1
Example 4:
Input: nums = [1,1,2]
Output: 1
'''
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
'''
#iterate through the list of nums
#swap the number i with the number in [i-1]th position
Time complexity - O(n)
Space complexity - O(1)
'''
#Code
for i in range(len(nums)):
while nums[i] != i+1 :
val = nums[i]
if val == nums[val-1]:
break
nums[val-1], nums[i] = val, nums[val-1]
return nums[-1]
'''
#Time complexity = O(n)
#Space complexity = O(n)
'''
'''
#Code
num_set = set()
for num in nums:
if num in num_set:
return num
num_set.add(num)
'''