二刷

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int[] res = new int[2] ;
        for(int i=0;i<numbers.length;i++){
            if(map.containsKey(numbers[i])){
                res = new int[]{map.get(numbers[i])+1,i+1};
             }
             else{
                 map.put(target-numbers[i],i);
             }
        }
        return res;
    }
}

1.哈希哈希哈希

import java.util.*;


public class Solution {
    /**
     * 
     * @param numbers int整型一维数组 
     * @param target int整型 
     * @return int整型一维数组
     */
    public int[] twoSum (int[] numbers, int target) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        int[] res = new int[2];
        for(int i=0; i<numbers.length ; i++){
            if(map.containsKey(target - numbers[i])){
                int small;
                int large;
                if( i> map.get(target - numbers[i])){
                    large = i;
                    small =  map.get(target - numbers[i]);
                }
                else{
                    small = i;
                    large =  map.get(target - numbers[i]);
                }
                res[0] = small+1;
                res[1] = large+1;
                return res;
            }
            else if (!map.containsKey(numbers[i])){
                map.put(numbers[i],i);
            }
        }
        return res;
    }
}