classSolution{ publicint[] twoSum(int[] nums, int t) { // t = a + b // 遍历时查找a,把找过的a放到hash,从找过的a里边查找b,这样只需遍历一次数组 Map<Integer, Integer> aHash = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int a = nums[i], b = t - a; if(aHash.containsKey(b)) { returnnewint[]{i, aHash.get(b)}; } aHash.put(a, i); } returnnewint[]{}; } }