LeetCode:1_Two_Sum | 两个元素相加等于目标元素 | Medium

简介: 题目: Given an array of integers, find two numbers such that they add up to a specific target number.

题目:

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2 

函数原型:
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
    }
};

 

解题思路:

1、暴力法:两个for循环遍历,时间复杂度为O(N^2),会超时。

2、排序法:这里有两种思路:

  1)排好序后,利用区间法来计算两个数的和(两个指针分别指向首尾,逐步向中间收缩)

  2)排好序后,固定一个元素a[i],在余下的数中查找target - a[i],查找可用二分查找法,时间复杂度为O(lgn)。

该种方法的时间复杂度为O(nlgn)。

注意:这种方法由于采用了排序,故每个数的index会改变,所以,必须将每个数和它的index进行关联,我们第一时间想到map,但是map不允许有重复的元素出现,故不合适。进而可以想到结构体,每个数有两个属性:value和index,这样就搞定了。

3、hashtable法:时间复杂度降为O(N)。思想来自排序法的第一种思路,这种方法用到了查找,总所周知,查找最快的方法就是采用hash表。但是前面也说过,hash不能存储重复的元素,比如(0,3,2,0),只存储3个元素,那查找后就无法得到正确答案。这个时候就需要想一种方法来避免这种情况,我们可以这样来做:来一个元素a[i],我们检查它是否在hash表中,不在就插入,然后检查target-a[i]是否在表中,如果在,得到结果。这样就可以得到我们想要的结果,千万不要把所有元素都插入了,再来查找,不然就得不到答案。

 

代码展示:

排序法:(第一种思路)

 1 //方法一:排序法
 2 struct SNode {
 3     int value;
 4     int pos;
 5 };
 6 
 7 bool cmp(SNode a, SNode b)
 8 {
 9     return a.value < b.value;
10 }
11 
12 vector<int> twoSum(vector<int>& nums, int target) {
13     int n = nums.size();
14     
15     vector<int> vecRet;
16     vector<SNode> vecNode;
17     
18     for (int i=0; i < n; i ++) {
19         SNode temp;
20         temp.value = nums[i];
21         temp.pos = i+1;
22         vecNode.push_back(temp);
23     }
24     sort(vecNode.begin(),vecNode.end(), cmp);
25 
26     int i = 0, j = n-1;
27     while(i < j) {
28         int sum = vecNode[i].value + vecNode[j].value;
29         if (sum == target) {
30             if(vecNode[i].pos < vecNode[j].pos){
31                 vecRet.push_back(vecNode[i].pos);
32                 vecRet.push_back(vecNode[j].pos);
33                 break;
34             }
35             if (vecNode[i].pos > vecNode[j].pos) {
36                 vecRet.push_back(vecNode[j].pos);
37                 vecRet.push_back(vecNode[i].pos);
38                 break;
39             }
40         }
41         else if (sum > target)
42             j--;
43         else
44             i ++;
45     }
46     return vecRet;
47 }

 

hashtable法:

 

 1 //方法二:hashtable法
 2 vector<int> twoSum1(vector<int>& nums, int target) 
 3 {
 4     int i, sum;
 5     vector<int> results;
 6     map<int, int> hmap;
 7     for(i=0; i<nums.size(); i++){
 8         if(!hmap.count(nums[i])){
 9             hmap.insert(pair<int, int>(nums[i], i));
10         }
11         if(hmap.count(target-nums[i])){
12             int j=hmap[target-nums[i]];
13             if(j<i){
14                 results.push_back(j+1);
15                 results.push_back(i+1);
16                 return results;
17             }
18         }
19     }
20     return results;
21 }

 

目录
相关文章
|
1月前
|
算法 Java
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
[Java·算法·简单] LeetCode 27. 移除元素 详细解读
23 1
|
1月前
|
算法 C语言
【C语言】Leetcode 27.移除元素
【C语言】Leetcode 27.移除元素
21 0
【C语言】Leetcode 27.移除元素
|
1月前
|
C++
两种解法解决 LeetCode 27. 移除元素【C++】
两种解法解决 LeetCode 27. 移除元素【C++】
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
1月前
|
算法
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
LeetCode[题解] 1261. 在受污染的二叉树中查找元素
16 1
|
4天前
|
算法
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
代码随想录算法训练营第五十七天 | LeetCode 739. 每日温度、496. 下一个更大元素 I
9 3
|
8天前
|
算法
【力扣】169. 多数元素
【力扣】169. 多数元素
|
16天前
|
算法
每日一题:LeetCode-LCR 179. 查找总价格为目标值的两个商品
每日一题:LeetCode-LCR 179. 查找总价格为目标值的两个商品
|
1月前
|
存储 JavaScript
leetcode82. 删除排序链表中的重复元素 II
leetcode82. 删除排序链表中的重复元素 II
22 0
|
1月前
leetcode83. 删除排序链表中的重复元素
leetcode83. 删除排序链表中的重复元素
10 0

热门文章

最新文章