class Solution {
    // 1.暴力求解,两重for循环分别代表起始元素
    // 2.用HashMap,时间复杂度为O(n),空间复杂度为O(n);
    // 两个数之和 = target 那么就是target - 其中一个数 = 另外一个target
    // 一重for循环,遍历整个数组,将出现过的值加入到hashmap中,key为值,value为索引
    // 对于当前元素,hashmap.contiansKey(target - value) ,则找到两个数
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        HashMap<Integer, Integer> hs = new HashMap();
        for (int i = 0; i < nums.length; i++) {
            int value = target - nums[i];
            if (hs.containsKey(value)) {
                res[1] = i;
                res[0] = hs.get(value);
                break;
            }
            hs.put(nums[i],i);
        }
        return res;
    }
   
}