fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
  15.  
  16. class Solution {
  17. public int[] twoSum(int[] nums, int target) {
  18.  
  19. //we will use map
  20.  
  21.  
  22.  
  23. HashMap<Integer , Integer> map = new HashMap<Integer , Integer>();
  24.  
  25.  
  26. int[] ans = new int[2];
  27.  
  28.  
  29.  
  30. for(int i = 0 ; i<nums.length ; i++){
  31.  
  32. if(map.containsKey( target - (nums[i]) ) ){
  33.  
  34. ans[0] = i;
  35. ans[1] = map.get(target - (nums[i]));
  36.  
  37. return ans;
  38.  
  39. }else{
  40.  
  41. map.put((nums[i]) , i);
  42.  
  43.  
  44. }
  45.  
  46.  
  47. }
  48.  
  49.  
  50. return ans;
  51.  
  52.  
  53.  
  54.  
  55. }
  56. }
Success #stdin #stdout 0.08s 52584KB
stdin
Standard input is empty
stdout
Standard output is empty