1. Two Sum

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

 (hashmap && two pointers )

 

two sum 
with duplicate number返回所有的可能的index pairs,我用的是map<Integer, Set<Integer>>


https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/K%20Sum.java





package com.jetbrains;

import java.util.*;

public class Main {


    public List<List<Integer>> twoSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        HashMap<Integer, List<Integer>> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (!map.containsKey(nums[i])) {
                map.put(nums[i], new ArrayList<>());
            }
            map.get(nums[i]).add(i);

            if (map.containsKey(target - nums[i])) {
                for (int candidate : map.get(target - nums[i])) {
                    if(i != candidate){
                        result.add(Arrays.asList(candidate, i));
                    }
                }

            }
        }
        return result;
    }


    public static void main(String[] args) {


        Main solution = new Main();

        int target = 10;
        int[] nums = new int[]{0, 8, 9, 8, 2, 4, 5, 1, 7, 7, 2, 5, 10};


        List<List<Integer>> result = solution.twoSum(nums, target);
        System.out.println("the result we got from this code is " + result);


    }
}


the result we got from this code is [[1, 4], [3, 4], [2, 7], [1, 10], [3, 10], [6, 11], [0, 12]]


原题

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

 

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            if(!map.containsKey(nums[i])){
                map.put(nums[i], new ArrayList<Integer>());
            }
            map.get(nums[i]).add(i);
            
            int num1 = nums[i];
            int num2 = target - num1;
            if(num1 != num2 && map.containsKey(num2)){
                result[0] = i;
                result[1] = map.get(num2).get(0);
            }
            if(num1 == num2 && map.get(num1).size() > 1){
                result[0] = map.get(num2).get(0);
                result[1] = map.get(num2).get(1);
            }
        }
        return result;
    }
}

 

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

posted on 2018-07-18 08:26  猪猪&#128055;  阅读(87)  评论(0)    收藏  举报

导航