题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
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<>(); for (int i = 0; i < numbers.length; i++) { if(map.containsKey(numbers[i])){ return new int[]{map.get(numbers[i])+1,i+1}; } map.put(target-numbers[i],i); } return null; } }