Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. public class Solution {
  2.  
  3. public int[] solution;
  4.  
  5. public int[] twoSum(int[] nums, int target) {
  6.  
  7. HashMap hashmap = new HashMap();
  8.  
  9. for(int i=0; i < nums.length; i++){
  10. hashmap.put(nums[i], i);
  11. }
  12.  
  13. for(int i=0; i < nums.length; i++){
  14. int another = target - nums[i];
  15. if(hashmap.containsKey(another) && (int)hashmap.get(another) !=i){
  16. int another_index;
  17. another_index = (int)hashmap.get(another);
  18. solution = new int[2];
  19. solution[0] = i;
  20. solution [1] = another_index;
  21. return solution;
  22. }
  23. }
  24. return nums;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement