Accepted Java O(n) Solution


  • 0
    S

    public class Solution {
    public int[] twoSum(int[] num, int target)
    {
    int result[]=new int[2];
    Map<Integer,Integer> hm=new HashMap<Integer,Integer>();
    for(int i=0;i<num.length;i++)
    if(hm.containsKey(target-num[i]))
    {
    result[0]=hm.get(target-num[i]);
    result[1]=i;
    }
    else
    hm.put(num[i],i);
    return result;
    }
    }


Log in to reply
 

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