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.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution 1:

 class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> myMap;
vector<int> result; for(size_t i = ; i < nums.size(); ++i)
{
myMap[nums[i]] = i;
} for(size_t i = ; i < nums.size(); ++i)
{
const int gap = target - nums[i];
auto it = myMap.find(gap);
if(it != myMap.end() && it->second != i)
{
result.push_back(i);
result.push_back(myMap[gap]);
break; // Assume that each input would have
// exactly one solution
}
}
return result;
}
};

Solution 2:  暴力查找,超时

 class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> result;
for(size_t i = ; i < nums.size(); ++i)
{
for(size_t j = i + ; j < nums.size(); ++j)
{
if(nums[i] + nums[j] == target)
{
result.push_back(i);
result.push_back(j);
break;
}
}
}
return result;
}
};

Solution 3:  先排序,然后左右夹逼

 class Solution
{
public:
vector<int> twoSum(vector<int>& nums, int target)
{
vector<int> result;
vector<Node> num;
for(size_t i = ; i < nums.size(); ++i)
{
Node temp;
temp.value = nums[i];
temp.pos = i;
num.push_back(temp);
}
sort(num.begin(), num.end(), cmp);
for(size_t i = , j = num.size() - ; i != j; )
{
int sum = num[i].value + num[j].value;
if(sum == target)
{
int smallPos = min(num[i].pos, num[j].pos);
int largePos = max(num[i].pos, num[j].pos);
result.push_back(smallPos);
result.push_back(largePos);
break; // 找到解后,中断
}
else if(sum > target) --j;
else ++i;
}
return result;
}
private:
struct Node
{
int value;
int pos;
};
static bool cmp(const Node &a, const Node &b)
{
return a.value < b.value;
}
};

two Sum ---- LeetCode 001的更多相关文章

  1. LeetCode #001# Two Sum(js描述)

    索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...

  2. 【JAVA、C++】LeetCode 001 Two Sum

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

  3. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  4. Path Sum [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for bin ...

  5. Leetcode 001. 两数之和(扩展)

    1.题目要求 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 2.解法一:暴力法(for*for,O(n*n)) ...

  6. leetcode 001

    1 Two Sum Difficulty: Easy The Link: https://leetcode.com/problems/two-sum/description/ Description ...

  7. 39. Combination Sum - LeetCode

    Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2, ...

  8. Minimum Path Sum [LeetCode]

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. Nested List Weight Sum -- LeetCode 339

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

随机推荐

  1. Anagrams [LeetCode]

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  2. 开发完iOS应用,接下去你该做的事

    iOS专项总结 关于 analyze Clang 静态分析器 Slender Faux Pas Warning Leaks Time Profiler 加载时间 iOS App启动过程 帧率等 如何优 ...

  3. PHP生成word的三种方式

    摘要: 最近工作遇到关于生成word的问题 现在总结一下生成word的三种方法. btw:好像在博客园发表博客只要是标题带PHP的貌似点击量都不是很高(哥哥我标题还是带上PHP了),不知道为什么,估计 ...

  4. MATLAB 生成数据保存至文件

    % load pyrim % NumTrain = 50; % load machine %NumTrain = 150; % load housing % NumTrain = 300; % loa ...

  5. WordPress怎么在页面上添加目录

    要实现的如下功能,在页面上添加一个文章目录: 步骤:   1)在wordpress中,在Posts----Categories中建立目录, 2) 3)add new post,指定post所属的cat ...

  6. HTML5标签学习之~~~

    <article> 标签 article 字面意思为“文章”.在web页面中表现为独立的内容,如一篇新闻,一篇评论,一段名言,一段联系方式.这其中包括两方面,一为整个页面的主旨内容,另外就 ...

  7. BZOJ2721 [Violet 5]樱花

    先令n! = a: 1 / x + 1 / y = 1 / a  =>  x = y * a / (y - a) 再令 k = y - a: 于是x = a + a ^ 2 / k  => ...

  8. sql server还原数据库文件(.bak)常见问题解决办法笔记

    还原的时候出现错误:备份集中的数据库备份与现有的数据库不同 SQL Server 2005数据库还原出错错误具体信息为:备份集中的数据库备份与现有的A数据库不同 具体操作如下:第一次:新建了数据库A, ...

  9. 创建论坛Discuz

    下载discuz! mkdir /data/wwwcd /data/wwwwget  http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_GB ...

  10. Google十大惊人产品

    国外资讯网站BusinessInsider刊文细数了谷歌惊世骇俗的十大产品,范围从无人驾驶汽车到太空电梯再到高空风力发电,每一项都令人无限神往,充满未来感. 以下是谷歌十大惊人产品: 众所周知,谷歌并 ...