两数之和解题思路
Java(题目来源:LeetCode)Here

(1)建立哈希表Map<Integer, Integer> map = new HashMap<>();
其中map的key是nums[i],value是i(nums数组中的值以及它对应的数组下标)


(2)遍历nums数组并做判断
temp = target - i; if( map.containsKey(temp))
如果存在,直接返回
return new int[] { map.get(temp), i}; // 返回符合要求的数组下标值对
如果不存在,则map.put(nums[i], i);


(3)经过以上步骤没有得出答案,抛出异常
throw new IllegalArgumentException("No Solution.");

完整代码两数之和解题思路

相关文章: