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
| public class SetLikeDS { | |
| private List<Integer> list; | |
| private Map<Integer, Integer> map; | |
| public SetLikeDS() { | |
| list = new ArrayList<>(); | |
| map = new HashMap<>(); | |
| } | |
| public void insert(int num) { |
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
| import javafx.util.Pair; //available in Java 1.8 | |
| public class Solution { | |
| public static boolean twoSum(int[] a, int target) { | |
| Map<Integer, Pair> map = new HashMap<>(); | |
| for(int i=0; i<a.length; i++) { //O(n) where n=a.length | |
| if (!map.containsKey(a[i])) { //don't insert duplicates | |
| map.put(a[i], new Pair<Integer, Integer>(target-a[i], i)); | |
| //key is every unique element, value is a pair of numberToFind and position of element first found |