题目:

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.

从给定的一个整数数组中找出两个数,使得它们的和为target,并返回两个数在原数组中的下标

思路:

1. 对数组进行排序,为了方便获取排序后的原下标,采用multimap(map不允许重复的keys值)

multimap不能用 [key] = value的方式来行赋值,但是map可以

2. 用两个游标(i = 0, j = size - 1)分别从数组的两头开始,如果

1) nums[i] + nums[j] > target      大于目标值,则将j往前移一位

2) nums[i] + nums[j] < target      小于目标值,则将i往后移一位

3) nums[i] + nums[j] = target      完成~

 class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int nsize = nums.size();
multimap<int, int> _n_i;
for(int i = ; i < nsize; i++)
_n_i.insert(pair<int,int>(nums[i], i)); vector<int> result;
multimap<int, int>::iterator _it_begin = _n_i.begin();
multimap<int, int>::iterator _it_end = _n_i.end();
--_it_end;
while(true){
int tmp = _it_begin->first + _it_end->first;
if(tmp < target)
++_it_begin;
else if(tmp > target)
--_it_end;
else{
int i = _it_begin->second;
int j = _it_end->second;
if(i > j){
int _tmp = i;
i = j;
j = _tmp;
}
result.push_back(i);
result.push_back(j);
return result;
}
}
}
};

【LeetCode】1. Two Sum的更多相关文章

  1. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  2. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  4. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  5. 【LeetCode】#1 Two Sum

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

  6. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  7. 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  8. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  9. 【LeetCode】1. Two Sum 两数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...

  10. 【LeetCode】437. Path Sum III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...

随机推荐

  1. Linux学习笔记(23) Linux备份

    1. 备份概述 Linux系统需要备份的数据有/root,/home,/var/spool/mail,/etc及日志等其他目录. 安装服务的数据需要备份,如apache需要备份的数据有配置文件.网页主 ...

  2. 移动端_line-height问题

    如果把line-height加1px,iPhone文字就会下移,由于我们app的ios用户居多,并且android机型太多,不同机型也会显示不同,所以只能退而求其次了. line-height的兼容问 ...

  3. js jquery 实现点击按钮后,倒计时60秒才能再次点击发送验证邮件

    <input type="button" id="btn" value="免费获取验证码" /><script type= ...

  4. UVA 12232 Exclusive-OR(并查集+思想)

    题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...

  5. MySQL数据库监控

    MySQL MTOP由PHP和Python开发,所以监控机需要安装PHP运行环境和Python环境.需要的核心包如下: 1.MySQL 5.0及以上(用来存储监控系统采集的数据) 2.Apache 2 ...

  6. mysql数据库管理工具sqlyog在首选项里可以设置默认查询分页条数和字体,改写关键字大小写

    sqlyog设置一直习惯用sqlyog来管理mysql数据库,但有三个地方用得不是很爽:1.默认查询条数只有1000条经常需要勾选掉重新查询.2.自动替换关键字大小写,有时候字段名为关键字的搞成大写的 ...

  7. OAuth2.0 在 SSO中的应用~

    关于OAuth2.0的介绍,请看下面链接(讲的挺好的): http://blog.csdn.net/seccloud/article/details/8192707 我的理解: 一共四个角色,A:Cl ...

  8. 20145223《Java程序程序设计》第10周学习总结

    20145223<Java网络编程> 一.Java的网络编程 ·网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. ·java.net包中J2SE的API包含有类 ...

  9. 如何用distinct消除重复记录的同时又能选取多个字段值?

    http://www.cnblogs.com/warioland/archive/2012/05/30/2526128.html

  10. angular的post请求,SpringMVC后台接收不到参数值的解决方案

    http://www.ithao123.cn/content-6567265.html