Two Sumb
|Last edited: 2024-4-26
ID
1
Data Type
Hashmap / Hashset
Difficulty
Easy
Tags
Hash Table
URL
Completed Date
Feb 19, 2024
Preference
😰 Didn't Expect
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Example 2:
Example 3:
Constraints:
  • 2 <= nums.length <= 104
  • 109 <= nums[i] <= 109
  • 109 <= target <= 109
  • Only one valid answer exists.
Follow-up:
Can you come up with an algorithm that is less than time complexity?
 

思路

关键点在于用存了再匹配的方法,因为最终的结果已经知道了,所以只需要
结果数-当前数=仍要寻找的数
只要找到一个数==仍要寻找的数,就完成了