[LeetCode] Challenge log 1

1. Two Sum

https://leetcode.com/problems/two-sum/description/

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Keywords: hash table, dictionary
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
n = len(nums)
ht = {}
for i in range(n):
temp = target - nums[i]
if ht.__contains__(temp) and ht[temp] != i:
return [ht[temp], i]
ht[nums[i]] = i
return None
0%