class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
dic_value_index = {}
index_1 = int ()
index_2 = int ()
for index, value in enumerate(nums):
dic_value_index [value] = index
for item in nums:
sec_num = target - item
if sec_num in dic_value_index:
if nums.index(item) != dic_value_index[sec_num]: # if they are not the same number
index_1 = nums.index(item)
index_2 = dic_value_index[sec_num]
break
return ([index_1, index_2])

Key of this solution:

1. enumerate (list_name) will walk through the index and the value;

2. put the value and index into the dictinary

3. search whether the sec_num in the dictionary (only key can be searched? )

4. if yes, the the value is the index of the sec_num

5. check whether the sec_num is actually the first number, if not , then return the index.

### Like this:

Like Loading...

*Related*