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. [转载]SoapUI 参数化&数据库连接

    引用自 : http://www.cnblogs.com/liulinghua90/p/4954045.html 如果是没有代码能力的小白,要利用工具进行接口测试的时候,经常会遇到接口地址 或者接口参 ...

  2. openCV1

    openCV是一个提供有C++ , android , python的开源图像处理的类库 中文论坛的网址是http://www.opencv.org.cn/forum.php?mod=forumdis ...

  3. Permutations [LeetCode]

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. 获取图片颜色的rgb,以供css设计背景颜色

    ColorPix

  5. Neo4j图数据库简介和底层原理

    现实中很多数据都是用图来表达的,比如社交网络中人与人的关系.地图数据.或是基因信息等等.RDBMS并不适合表达这类数据,而且由于海量数据的存在,让其显得捉襟见肘.NoSQL数据库的兴起,很好地解决了海 ...

  6. PDF 补丁丁 0.4.1 版将增加嵌入中文字库的功能

    有不少用户反映,部分老 PDF 文件由于在制作时没有嵌入字库,导致该文件在某些阅读器上显示为乱码.即使他们用 Acrobat 嵌入了相应的字库,文件仍然无法正确显示. 这些老 PDF 看起来具有如下相 ...

  7. VS2005保存文件很慢

    VS2005出了点毛病,边的出奇的慢,简直不可忍受. 症状是:保存文件很慢,哪怕是修改一个变量,也要等上大概20秒 保存文件的时候,VS2005会在局域网内寻找一个主机当这个主机不在线的时候vs200 ...

  8. heartbeat安装

    wget ftp://mirror.switch.ch/pool/1/mirror/scientificlinux/6rolling/i386/os/Packages/epel-release-6-5 ...

  9. UI UIView

    课程内容:   一.iOS概述 2007年1月9日Macworld大会上公布iPhone OS系统,2010WWDC大会上改名为iOS   二. UI编程概述 UI的本意是用户界面,是英文User和 ...

  10. 面试题目-atof与ftoa

    /////////////////////////////////////////////////////////////////////////////// // // FileName : ato ...