package com.app.main.LeetCode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* * id = 1
* * level = easu
* Created with IDEA
* author:Dingsheng Huang
* Date:2019/9/4
* Time:下午5:06
*
* 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].
*
*/
public class TwoSum {
public int[] twoSum(int[] nums, int target) {
int len = nums.length;
Set<Integer> set = new HashSet<>();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < len; i++) {
map.put(target - nums[i], i);
}
for (int j = 0; j < len; j++) {
if (map.containsKey(nums[j]) && j != map.get(nums[j])) {
return new int[] {j, map.get(nums[j])};
}
}
return new int[] {};
}
}