-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThreeSum.py
87 lines (70 loc) · 2.28 KB
/
ThreeSum.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Algorithms Pracitice: ThreeSum question
A program with cubic running time. Reads n integers and counts the number of
triples that sum to exactly 0 (Intergers are different in this array)
算法训练: 三元和的问题
统计所有和为 0 的三整数元组的数量(数组内整数均不相等)
"""
import sys
sys.path.append('../')
from BinarySearch import BinarySearch
from generator import *
class ThreeSum(object):
@staticmethod
def count(input):
start = time()
cnt = 0
result = []
for i in range(len(input)):
for j in range(i+1, len(input)):
for k in range(j+1, len(input)):
if input[i] + input[j] + input[k] == 0:
result.append([input[i], input[j], input[k]])
cnt += 1
end = time()
print('time: %s, count: %d' % (end - start, cnt))
@staticmethod
def countFast(input):
start = time()
cnt = 0
result = []
input.sort()
for i in range(len(input)):
for j in range(i+1, len(input)):
if BinarySearch(input, -input[i]-input[j]) > j:
result.append([input[i], input[j], -input[i]-input[j]])
cnt += 1
end = time()
print('time: %s, count: %d' % (end - start, cnt))
class TwoSum(object):
@staticmethod
def count(input):
start = time()
cnt = 0
result = []
for i in range(len(input)):
for j in range(i+1, len(input)):
if input[i] + input[j] == 0:
result.append([input[i], input[j]])
cnt += 1
end = time()
print('time: %s, count: %d' % (end - start, cnt))
@staticmethod
def countFast(input):
start = time()
cnt = 0
result = []
input.sort()
for i in range(len(input)):
if BinarySearch(input, -input[i]) > i:
result.append([input[i], -input[i]])
cnt += 1
end = time()
print('time: %s, count: %d' % (end - start, cnt))
data = noRepeat(getRandomNumbers(-1000, 1000, 100))
TwoSum.count(data)
TwoSum.countFast(data)
ThreeSum.count(data)
ThreeSum.countFast(data)