Runtime Beats 100% java submissions - simple solution


  • 0
    R
    public static int[] twoSum(int[] nums, int target){
    
      int[] result = new int[2];
      Map<Integer, Integer> table = new LinkedHashMap<>();
    
      for(int i=0; i < nums.length; i += 1)
        table.put(i, nums[i]);
    
      for(int key : table.keySet()){
    
          int desired = target - table.get(key);
          if(table.containsValue(desired) && lookUp(table, desired, key) != -1){
              
               result[0] = key;
               result[1] =  lookUp(table, desired, key);
               return result; 
          }
      }
           return result;
    }
    public static int lookUp(Map<Integer,Integer> table, int desired, int index){
    
          for(int key : table.keySet() )
                if( (table.get(key) == desired) && index != key)
                            return key;
          return -1;
    }
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.