LeetCode:1_Two_Sum | 两个元素相加等于目标元素 | Medium
题目:
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 }
作者:公众号「Linux云计算网络」,专注于Linux、云计算、网络领域技术干货分享
出处:https://www.cnblogs.com/bakari/p/4871254.html
本站使用「署名 4.0 国际」创作共享协议,转载请在文章明显位置注明作者及出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?