-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.two-sum.py
More file actions
51 lines (49 loc) · 1.22 KB
/
Copy path1.two-sum.py
File metadata and controls
51 lines (49 loc) · 1.22 KB
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
47
48
49
50
#
# @lc app=leetcode id=1 lang=python3
#
# [1] Two Sum
#
# https://leetcode.com/problems/two-sum/description/
#
# algorithms
# Easy (44.51%)
# Likes: 11800
# Dislikes: 406
# Total Accepted: 2.1M
# Total Submissions: 4.7M
# Testcase Example: '[2,7,11,15]\n9'
#
# Given an array of integers, return indices of the two numbers such that they
# add up to a specific target.
#
# You may assume that each input would have exactly one solution, and you may
# not use the same element twice.
#
# Example:
#
#
# Given nums = [2, 7, 11, 15], target = 9,
#
# Because nums[0] + nums[1] = 2 + 7 = 9,
# return [0, 1].
#
#
#
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# dic = {nums[i] : i for i in range(len(nums))}
# print(dic)
# for i in range(len(nums)):
# print(i)
# if target-nums[i] in dic:
# if i == dic.get(target-nums[i]):
# continue
# return [i,dic.get(target-nums[i])]
# return [0,0]
dic = {}
n = len(nums)
for i in range(n):
if target-nums[i] in dic:
return [dic[target-nums[i]], i]
else:
dic[nums[i]] = i