Skip to content

Instantly share code, notes, and snippets.

View gabelorenzo's full-sized avatar

Gabe Lorenzo gabelorenzo

View GitHub Profile
@gabelorenzo
gabelorenzo / LeetCodeTwoSum.py
Last active May 7, 2022 07:52
Two Sum (Python)
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:
@gabelorenzo
gabelorenzo / TwoSum.java
Last active May 7, 2022 07:53
Two Sum (Java)
/**
* 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.