Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
——————————————————————————————————
框架
1 | class Solution { |
1. 暴力求解
直接遍历,O(n^2)
1 | vector<int> twoSum(vector<int>& nums, int target) { |
2. hash/对数级查找
遍历nums的数据,每遍历一个数据x,去hash表中查找有无target - x的数据,并将该数据x插入到hash表中。
hash表中,key储存数据x或者数据x需要的数据target - x,value储存其index
ash表的查找可以看作是O(1)(如果不发生碰撞的话)(map是O(logn)),所以复杂度为O(n)或O(nlogn)
后补:map
的实现依赖于红黑树,内部是有序的,实现了对数级别的查找,以下代码是依靠map
实现的。map
与hash表没有关系。hash表用更大的空间实现了更快的查找:O(1)
。
hash表可以使用unordered_map
,由hash函数实现,所以查找插入和删除都是O(1)
的。
也正因为如此,unordered_map
的内部元素是无序的,而map
内部是有序的。
至于hash-map
,它与unordered_map
功能相同,但是不属于STL,是历史原因,使用unordered_map
即可。
1 | vector<int> twoSum(vector<int>& nums, int target) { |