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

法I:先快速排序,时间复杂度O(nlogn),然后再二分法查找,时间发杂度O(nlogn)。这样比乱序时查找O(n2)要快。

struct MyStruct {
int data;
int pos;
MyStruct(int d, int p){
data = d;
pos = p;
}
bool operator < (const MyStruct& rf) const{//参数用引用:避免占用过大内存,并且避免拷贝;用const防止改变操作数
if(data < rf.data) return true;
else return false;
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<MyStruct> myNums;
vector<int> ret;
int idx1 = , idx2;
for(int i = ; i < nums.size(); i++){
MyStruct* num = new MyStruct(nums[i],i);
myNums.push_back(*num);
} sort(myNums.begin(),myNums.end());
while(!binarySearch(myNums, target-myNums[idx1].data, idx1+, nums.size()-, idx2)){
idx1++;
} ret.clear();
if(myNums[idx1].pos < idx2){
ret.push_back(myNums[idx1].pos+);
ret.push_back(idx2+);
}
else{
ret.push_back(idx2+);
ret.push_back(myNums[idx1].pos+);
}
return ret;
} bool binarySearch(const vector<MyStruct>& nums, const int& target, int start, int end, int& targetPos)
{
if(end - start == ){
if(nums[start].data == target){
targetPos = nums[start].pos;
return true;
}
else if(nums[end].data == target){
targetPos = nums[end].pos;
return true;
}
else return false;
} int mid = (start + end) >> ;
if(nums[mid].data == target){
targetPos = nums[mid].pos;
return true;
} if(nums[mid].data > target && start != mid && binarySearch(nums, target, start, mid, targetPos)) return true;
if(binarySearch(nums, target, mid, end, targetPos)) return true; return false;
}
};

法II:hash table. unordered_map的内部实现是先将key通过hash_value()得到其hash值,相同hash值的key以红黑树排列,查找时间是O(logn),建树的时间类似建大顶堆,时间复杂度是O(n)

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> ret;
unordered_map<int,int> map;
unordered_map<int,int>::iterator it; for(int i = ; i < nums.size(); i++){
it = map.find(target-nums[i]);
if(it!=map.end()){
ret.push_back(it->second);
ret.push_back(i);
return ret;
} map.insert(make_pair(nums[i],i));
}
return ret;
}
};

最新文章

  1. JUnit 4 与 TestNG 对比
  2. Android中的Service小结
  3. C#程序以管理员身份运行
  4. Java多线程编程核心技术--Lock的使用(一)
  5. Windows 下, SetTimer 定时器的研究.
  6. HNU 12826 Balloons Colors
  7. 不通过App Store,在iOS设备上直接安装应用程序(转)
  8. mysql导入导出
  9. ls -l 列表信息详解
  10. PHP字符串拼接与MySQL语句
  11. hdr_beg(host) hdr_reg(host) hdr_dom(host)
  12. ArcEngine10在VS2010中编译问题
  13. Web API CSRF保护实现
  14. ubuntu 12.04 安装snort acidbase相关注意事项
  15. java aio nio bio
  16. Hibernate框架入门
  17. C语言-第6次作业
  18. java中String类为什么不可变?
  19. zk理解(转载自邬兴亮---www.cnblogs.com/wuxl360/p/5817471.html)
  20. mysql分类和事务回滚

热门文章

  1. [转]链接中 href=&#39;#&#39; 和 href=&#39;###&#39; 的区别以及优缺点
  2. s3express截图安装教程
  3. PLSQL导出表结构
  4. Access-Control-Allow-Origin 跨域问题
  5. Centos7扩展磁盘空间(LVM管理)
  6. ZooKeeper系列(2) 安装部署 (转)
  7. 【Linux_Unix系统编程】chapter7 内存分配
  8. Spark分析之Master、Worker以及Application三者之间如何建立连接
  9. webpack(4)--module
  10. SQL Server数据库定时备份解决方案