两数之和解题思路
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.");

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

相关文章:

  • 2021-09-09
  • 2022-12-23
  • 2021-07-26
  • 2021-11-28
  • 2022-02-28
  • 2021-08-11
  • 2022-03-06
  • 2021-09-08
猜你喜欢
  • 2021-09-29
  • 2021-08-24
  • 2021-07-15
  • 2021-07-21
  • 2021-04-06
  • 2021-10-22
  • 2021-11-25
相关资源
相似解决方案