Skip to content

Commit b858d6c

Browse files
authored
Merge pull request doocs#18 from zhanary/patch-2
Solution2.py
2 parents 11f480c + 75c3be0 commit b858d6c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

solution/001.Two Sum/Solution2.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#time 32ms
2+
class Solution(object):
3+
def twoSum(self, nums, target):
4+
buff = {}
5+
for i in range(len(nums)):
6+
if nums[i] in buff: #如果这个数在字典里,证明之前有数可以和他匹配
7+
return buff[nums[i]],i #返回之前留信息的数的编号,和当前这个数的编号
8+
else:
9+
buff[target - nums[i]] = i #这个数字不在字典里,那么就存一下他需要匹配的数,并记录编号
10+

0 commit comments

Comments
 (0)