LeetCode1

#

Example:

1
2
3
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
for i in range(length):
another = target - nums[i]
ii = [idx for idx, j in enumerate(nums) if another == j and idx != i]
if ii == []:
continue
else:
ii.insert(0, i)
return ii

很显然,代码不够优雅,虽然是比较容易想到的方式。

人艰不拆,生活不易