题目:

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

C++解答一(传统方法,Runtime: 19 ms):

 struct Node
{
int num;
int pos;
}; bool cmp(Node a,Node b)
{
return a.num<b.num;
} class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) { vector<int> result;
vector<Node> tmp;
Node a; for(int i = ;i<numbers.size();i++)
{
a.pos = i+;
a.num = numbers[i];
tmp.push_back(a);
} sort(tmp.begin(),tmp.end(),cmp);
int j = tmp.size()-;
int tmpvalue = ; for(int i = ;i<tmp.size();)
{
tmpvalue = tmp[i].num+tmp[j].num;
if(tmpvalue==target)
{
if(tmp[i].pos<tmp[j].pos)
{
result.push_back(tmp[i].pos);
result.push_back(tmp[j].pos);
}
else
{
result.push_back(tmp[j].pos);
result.push_back(tmp[i].pos);
} break;
}
else if(tmpvalue>target)
{
j--;
}
else
{
i++;
}
} return result;
}
};

C++ 解答二(巧妙方法,Runtime: 35 ms):

 class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
int i, sum;
vector<int> results;
map<int, int> hmap;
for(i=; i<numbers.size(); i++){
if(!hmap.count(numbers[i])){
hmap.insert(pair<int, int>(numbers[i], i));
}
if(hmap.count(target-numbers[i])){
int n=hmap[target-numbers[i]];
if(n<i){
results.push_back(n+);
results.push_back(i+);
return results;
} }
}
return results;
}
};

LeetCode 之 TwoSum的更多相关文章

  1. LeetCode #1 TwoSum

    Description Given an array of integers, return indices of the two numbers such that they add up to a ...

  2. Leetcode 1——twosum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  3. leetcode之twosum

    class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...

  4. leetcode ----ARRAY TWOSUM

    代码的(判断nums[i]或者是target-nums[i]都可以):

  5. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  6. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  7. ANDROID学习书单

    Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport   This reposito ...

  8. Android技能树

    第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...

  9. LeetCode初体验—twoSum

    今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...

随机推荐

  1. 微信支付 php发送POST请求

    https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=20_1 <_xml> <mch_id>132</mc ...

  2. window异常处理——except_handler4以及栈展开分析

    以前在15pb学习时候在看雪论坛发的一篇精华帖. 主要是分析在try块中发生嵌套异常时候堆栈是如何平衡的. 就不复制过来了,给个链接http://bbs.pediy.com/showthread.ph ...

  3. JVM的JIT机制

    因为 JVM 的 JIT 机制的存在,如果某个函数被调用多次之后,JVM 会尝试将其编译成为机器码从而提高执行速度.

  4. 转+总结!! maven的package与install命令区别

    如果b项目依赖a项目,而a打了包(package),jar仅仅时打到了a项目的target下.这时编译b项目,还是会报错,找不到所依赖的a项目,说明b项目在本地仓库是没有找到它所依赖的a项目. 然后, ...

  5. C#连接EXCEL和ACCESS字符串2003及2007版

    97-2003版本 EXCEL Provider=Microsoft.Jet.OLEDB.4.0;Data Source=文件位置;ExtendedProperties=Excel 8.0;HDR=Y ...

  6. Notice公告

    把网页放大到125%食用效果更佳 考试题目的密码是ftp的密码 博猪很菜,菜得抠脚,如果写的有什么问题可以在评论区指出:-) 博猪qq:1755833514

  7. MongoDB-6: MongoDB索引

    一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...

  8. MongoDB-3: 查询(一)

    一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...

  9. 我的Android进阶之旅------>Java全角半角的转换方法

    一中文全角和半角输入的区别 1全角指一个字符占用两个标准字符位置 2半角指一字符占用一个标准的字符位置 3全角与半角各在什么情况下使用 4全角和半角的区别 5关于全角和半角 6全角与半角比较 二转半角 ...

  10. tomcat 配置文件 介绍

    [root@mysql logs]# cd ../conf/ [root@mysql conf]# ll总用量 228drwxr-x---. 3 root root 4096 11月 15 2018 ...