5ms java solution


  • 16
    V

    public class Solution {
    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int[] eresult = {0,0};
        
        for(int i = 0; i < nums.length; i++) {
            if(map.get(nums[i]) != null) {
                int[] result = {map.get(nums[i])+1,i+1};
                return result;
            }
            map.put(target-nums[i], i);
        }
        return eresult;
    }
    

    }


  • 0
    S
    This post is deleted!

  • 0
    G

    this solution is very impresive


  • 0
    L

    @vshldc said in 5ms java solution:

    public class Solution {
    public int[] twoSum(int[] nums, int target) {

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int[] eresult = {0,0};
        
        for(int i = 0; i < nums.length; i++) {
            if(map.get(nums[i]) != null) {
                int[] result = {map.get(nums[i])+1,i+1};
                return result;
            }
            map.put(target-nums[i], i);
        }
        return eresult;
    }
    

    }


Log in to reply
 

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