Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. public class Solution {
  2.  
  3. public int[] twoSUm(int[] numbers, int target) {
  4.  
  5. HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  6. int[] result = new int[2];
  7.  
  8. for (int i=0; i < numbers.length; i++) {
  9. if (map.containsKey(numbers[i])) {
  10. int index = map.get(numbers[i]);
  11. result[0] = index;
  12. result[1] = i;
  13. break;
  14. } else {
  15. map.put(target - numbers[i], i);
  16. }
  17. }
  18.  
  19. return result;
  20. }
  21. }
  22.  
  23. // O(1) - get and put operations of HashMap
  24. // O(n)
Add Comment
Please, Sign In to add comment