two sum
commit 059b3d34c2cc45c929245493a5d251bc87f2561c
1 changed files with 7 additions and 0 deletions
two_sum.py ADDED
View File
| @@ -0,0 +1,7 @@ | ||
| 1 | +def two_sum(nums, target): | |
| 2 | + seen = {} | |
| 3 | + for i, n in enumerate(nums): | |
| 4 | + if target - n in seen: | |
| 5 | + return [seen[target - n], i] | |
| 6 | + seen[n] = i | |
| 7 | + return [] | |