This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def twoSum(self, nums, target): | |
""" | |
:type nums: List[int] | |
:type target: int | |
:rtype: List[int] | |
""" | |
h = {} | |
for i, num in enumerate(nums): | |
n = target - num | |
if n not in h: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given an array of integers, return indices of the two numbers such that they add up to a specific target. | |
* NOTE: You may assume that each input would have exactly one solution, and you may not use the same element twice. | |
* | |
* @param nums An array of integers, containing exactly 1 pair of nums that add up to target | |
* @param target The integer that a pair of numbers in the array add up to | |
* @return The two indices containing integers that add up to target. | |
*/ | |
public int[] twoSum(int[] nums, int target) { | |
// Space complexity: O(n). The extra space required depends on the number of items stored in the hash table, which stores at most n elements. |