Given an integer array nums and an integer target value target, find the two integers in the array and the target value target and return their array subscripts.

You can assume that there is only one answer for each type of input. However, the same element in the array cannot be repeated in the answer.

You can return the answers in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9 output: [0,1] Example 2:

Input: nums = [3,2,4] target = 6 output: [1,2] example 3:

Input: nums = [3,3], target = 6

Tip:

2 <= nums.length <= 104-109 <= nums[I] <= 109-109 <= target <= 109

Answer: Thought: Map.containkey () = target (); containKey() = target ();

Code examples:

public static int[] sumOfTwoNumbers(int[] nums, int target) {
    if (nums == null) {
        return null;
    }
    if (nums.length < 2) {
        return null;
    }
    Map<Integer, Integer> map = new HashMap<>(8);
    for (int i = 0; i < nums.length; i++) {
        int temp = target - nums[i];
        if (map.containsKey(temp)) {
            return new int[]{map.get(temp), i};
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No results returned");
}
Copy the code

Test cases:

Public static void main(String[] args) {int[] sumOfTwoNumbers = sumOfTwoNumbers(new int[]{1,4,5}, 5); if (null ! = sumOfTwoNumbers) { System.out.println(sumOfTwoNumbers[0]+" , "+sumOfTwoNumbers[1]); }}Copy the code