-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy paththreeSum.py
More file actions
28 lines (22 loc) · 709 Bytes
/
threeSum.py
File metadata and controls
28 lines (22 loc) · 709 Bytes
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
'''
Author: Tushar Nitave
'''
nums = [-1, 0, 1, 2, -1, -4]
hashTable = []
result = [] # result containing duplicate values
finalresult = [] # stores final result without duplicate values
for i in range(0, len(nums) - 2):
for j in range(i + 1, len(nums) - 1):
x = -(nums[i] + nums[j])
if (x in hashTable):
result.append([])
result[len(result) - 1].append(x)
result[len(result) - 1].append(nums[i])
result[len(result) - 1].append(nums[j])
else:
hashTable.append(nums[j])
for i in range(len(result)):
result[i].sort()
if not result[i] in finalresult:
finalresult.append(result[i])
print(finalresult)