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. (28)odoo中css可用颜色对照表

    颜色 颜色英文代码 形像颜色 HEX格式 RGB格式 LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩红 ...

  2. android:configChanges属性

    对android:configChanges属性,一般认为有以下几点: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏 ...

  3. 445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. JS模块式开发

    问题:js文件须严格保证加载顺序(比如上例的1.js要在2.js的前面),依赖性最大的模块一定要放到最后加载,当依赖关系很复杂的时候,代码的编写和维护都会变得困难! C语言中模块开发-include ...

  5. php不解析的排查步骤

    php不解析的排查步骤:1. /usr/local/apache2/bin/apachectl -M 看一下有没有加载libphp5.so2. 查看配置文件中是否有 AddType applicati ...

  6. php验证用户名是否以字母开头与验证密码

    验证用户名是否以字母开头与验证密码只能为数字和字母的组合代码三款三种常用验证函数 验证邮箱地址格式  验证密码只能为数字和字母的组合 验证用户名是否以字母开头代码哦,这是用户注册时或提交表单时会用的哦 ...

  7. NimBus一个好的开发框架

    NimbusKit是一个非常适合有经验的开发人员使用的iOS开发框架,具备完整的文档,并且提供了模块化的方式来解决iOS开发中的各种不同需求.最重要的是,该框架会经常添加一些新的组件和功能. Nimb ...

  8. 安卓Json介绍(转)。

    1.JSON(JavaScript Object Notation) 定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式, ...

  9. Windows系统安装时间

    http://www.45it.com/windowszh/201206/30693.htm 修改系统安装时间 开始" - "运行" - 输入"regedit& ...

  10. HDU 4548

    题意: 给你一个区间,问你这个区间中的数既是素数又是美素数的数有多少个?美素数:首先这个数本身必须是素数并且它的各位数字的和也是素数; 如29,本身是素数, 而且2+9 = 11也是素数, 所以它是美 ...