MCPcopy
hub / github.com/HuberTRoy/leetCode / threeSum

Method threeSum

Array/ThreeSum.py:45–170  ·  view source on GitHub ↗

:type nums: List[int] :rtype: List[List[int]]

(self, nums)

Source from the content-addressed store, hash-verified

43class Solution(object):
44
45 def threeSum(self, nums):
46 """
47 :type nums: List[int]
48 :rtype: List[List[int]]
49 """
50
51 sortedNums = nums
52 result = []
53 index_dict = {}
54 for i, d in enumerate(sortedNums):
55 try:
56 index_dict[d].append(i)
57 except KeyError:
58 index_dict[d] = [i]
59
60 sortedNums = sorted(set(sortedNums))
61
62 length = len(sortedNums) - 1
63 result = []
64 for i, data in enumerate(sortedNums):
65 if data > 0:
66 break
67 target = 0 - data
68 start = i + 1
69 end = length
70 while start < end:
71 if sortedNums[start] + sortedNums[end] == target:
72 # print(target)
73 result.append(sorted((sortedNums[start], sortedNums[end], data)))
74 # break
75 end -= 1
76 start += 1
77 continue
78
79 if sortedNums[start] + sortedNums[end] > target:
80 end -= 1
81 else:
82 start += 1
83
84 for i in index_dict:
85 if i == 0:
86 if len(index_dict.get(i)) >= 3:
87 result.append([0, 0, 0])
88 continue
89
90 if len(index_dict.get(i)) >= 2 and i != 0:
91 if index_dict.get(-2*i):
92 result.append(sorted((i,i,-2*i)))
93
94
95 return result
96 # for j in range(i+1, length):
97 # pass
98 # target = j - data
99 # (data, sortedNums[j], target-sortedNums[j])
100 # x = self.binarySearch(sortedNums, target, j+1)
101
102 # if x:

Callers 1

ThreeSum.pyFile · 0.80

Calls 1

getMethod · 0.80

Tested by

no test coverage detected