题目:

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. 在Salesforce中向Page Layout中添加Visualforce Page

    在Salesforce中可以向Object所对应的Layout中添加我们自定义的Visualforce Page. 此时的Visualforce Page与Asp.Net MVC中的Partial V ...

  2. 用Access作为后台数据库支撑。

    /// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...

  3. BZOJ 3282 Tree ——KD-Tree

    [题目分析] 明显的LCT维护连通性的题目. access的操作是比较巧妙的,可以把结点到根变成偏爱路径,而且保证了该点是链上深度最深的点. 而且需边的思想也很巧妙,保证了复杂度. 但是只能用于修改路 ...

  4. Codeforces Round #346 (Div. 2)

    前三题水 A #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; int main() { int ...

  5. set UVA 11136 Hoax or what

    题目传送门 题意:训练指南P245 分析:set维护,查询删除最大最小值 #include <bits/stdc++.h> using namespace std; typedef lon ...

  6. Python 学习笔记01

      print:直接输出 type,求类型 数据类型:字符串,整型,浮点型,Bool型     note01.py # python learning note 01   print('Hello w ...

  7. 《Getting Started with Storm》章节一 基础

    注:括号里的字,并且是(灰色)的,是我个人的理解,如有差错,欢迎交流 Storm是一个分布式的.可靠的.容错的数据流处理系统(流式计算框架,可以和mapreduce的离线计算框架对比理解).整个任务被 ...

  8. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  9. BZOJ2652 : 三角板

    首先旋转坐标系,假设$(x,y)$被$(X,Y)$遮挡等价于$X\leq x$且$Y\leq y$. 对于每种坐标系建立两棵线段树: 第一棵按$x$维护已经加入的点的$y$的最小值: 第二棵按$x$维 ...

  10. hdu1710 Binary Tree Traversals(二叉树的遍历)

    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjo ...