Created
October 31, 2016 14:11
-
-
Save nashid/3b16418737a3e1d198ab95e249a5dd09 to your computer and use it in GitHub Desktop.
Two Sum: Given an array of integers, find two numbers such that they add up to a specific target number
This file contains 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
import java.util.Map; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.HashSet; | |
public class TwoSum { | |
public static Set<Integer, Integer> twoSum(int targetSum, int input[]) { | |
Map<Integer, Integer> map = new HashMap<>(); | |
Set<Integer, Integer> results = new HashSet<>(); | |
for(int i=0; i< input.length; i++){ | |
if(map.containsKey(input[i])) { | |
results.add(input[i], targetSum-input[i]); | |
} | |
else { | |
map.put(sum-input[i],i); | |
} | |
} | |
} | |
public static void main(String args[]){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment