Java HashMap solution


  • 0
    L

    '''
    public class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
    Arrays.sort(nums);
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    List<List<Integer>> res = new LinkedList<>();
    for( int i=0; i<nums.length-2; i++ ) {
    int compl = 0 - nums[i];
    if( i>0 && nums[i] == nums[i-1]){
    continue;
    }
    map.clear();
    for( int j=i+1; j<nums.length; j++) {
    int innerCompl = compl - nums[j];
    boolean contain = map.containsKey(innerCompl);
    if (j < nums.length-1 && nums[j] == nums[j+1] ) {
    if ( !contain )
    map.put(nums[j],j);
    continue;
    }
    else {
    if( contain ) {
    res.add(Arrays.asList(nums[i],innerCompl,nums[j]));
    }
    }
    if (!contain)
    map.put(nums[j],j);
    }
    }
    return res;
    }
    }
    '''


Log in to reply
 

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