-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.py
46 lines (43 loc) · 1.35 KB
/
1.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
class Solution: # 这个的时间复杂度居然还是方法2的 1/15
def twoSum(self, nums, target):
self.nums = sorted(nums)
lenth = len(self.nums) - 1
count = 0
r_count = lenth
while True:
a = self.nums[count]
b = self.nums[r_count]
aw = a + b
if count == r_count:
count += 1
r_count = lenth
elif aw == target:
if a == b:
num_1 = nums.index(a)
nums.remove(a)
num_2 = nums.index(b)+1
return [num_1, num_2]
else:
return [nums.index(a), nums.index(b)]
elif aw < target:
count += 1
r_count = lenth
else :
r_count -= 1
class Solution:
def twoSum(self, nums, target):
b = nums
for i in nums:
aw = target - i
if aw in nums : # 判断在集合 这里耗时多
if nums.count(i) == 2 :
ix_1 = nums.index(i)
b.remove(i)
ix_2 = b.index(i) + 1
return [ix_1, ix_2]
elif i != aw:
return [nums.index(i), nums.index(aw)]
a = Solution()
b = [3,2,4]
c = 6
print(a.twoSum(b, c))