-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001两数之和.py
35 lines (32 loc) · 899 Bytes
/
001两数之和.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
# -*- coding:utf-8 -*-
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# 暴力法
if len(nums) <= 1:
return None
for i in range(len(nums)-1):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
return None
def twoSum1(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# 建一个Dict
if len(nums) <= 1:
return None
num_dict = {}
for i, num in enumerate(nums):
if (target-num) in num_dict:
return [num_dict[target - num], i]
if num not in num_dict:
num_dict[num] = i
return None