【题目】

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

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.





You may assume that each input would have exactly one solution.





Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

【题意】

1. 从所给的列表中找出两个数,这两个数的和与给定的target值同样。返回这两个值的位置(保持相对顺序)

2. 所给測试用例保证有且仅仅有一个唯一解

【思路】

1. 绑定值和相应的位置
复杂度O(n)

2. 对值进行升序排序 
复杂度O(nlogn)

3. 用两个指针p1,p2分别从前后两个方向逼近target。

当两指针之和小于target,则p1++

当两指针之和大于target。则p2--

【代码】

struct Node{
int index;
int value;
Node(){};
Node(int i, int v):index(i), value(v){};
}; bool compare(const Node &n1, const Node &n2){
return n1.value < n2.value;
} class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
vector<Node> nodes;
for(int i=0; i<numbers.size(); i++){
nodes.push_back(Node(i+1, numbers.at(i)));
}
sort(nodes.begin(), nodes.end(), compare); int p1 = 0;
int p2 = nodes.size() - 1;
vector<int> indexs;
while(p1 < p2){
int sum = nodes.at(p1).value + nodes.at(p2).value;
if(sum == target){
indexs.push_back(min(nodes.at(p1).index, nodes.at(p2).index));
indexs.push_back(max(nodes.at(p1).index, nodes.at(p2).index));
break;
}
else{
if(sum < target) p1++;
else p2--;
}
}
return indexs;
}
};

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

  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. LeetCode 算法题解 js 版 (001 Two Sum)

    LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  7. [leetCode][013] Two Sum 2

    题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  8. [LeetCode] #167# Two Sum II : 数组/二分查找/双指针

    一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...

  9. [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针

    一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...

  10. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

随机推荐

  1. logstash 处理多行

    2.2.2 多行事件编码: zjtest7-frontend:/usr/local/logstash-2.3.4/bin# ./plugin list | grep multi Ignoring ff ...

  2. mariadb启动

    systemctl start mariadb.service #启动MariaDBsystemctl stop mariadb.service #停止MariaDBsystemctl restart ...

  3. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  4. bzoj3293 [Cqoi2011]分金币&&bzoj1045 [HAOI2008]糖果传递

    Description 圆桌上坐着n个人,每人有一定数量的金币,金币总数能被n整除.每个人可以给他左右相邻的人一些金币,最终使得每个人的金币数目相等.你的任务是求出被转手的金币数量的最小值. Inpu ...

  5. js混淆 反混淆 在线

    js反混淆地址:http://www.bm8.com.cn/jsConfusion/ 在线javascript 混淆http://www.moralsoft.com/jso-online/hdojso ...

  6. 把DEDE的在线文本编辑器换成Kindeditor不显示问题

    在织梦论坛下载了[Kindeditor编辑器For DedeCMS],按照操作说明安装后,后台文章编辑的区域却显示空白,有人说不兼容V57版本,有人说不兼容gbk版本,我也纠结了很久,在网上找了很多版 ...

  7. JS正则表达式大全【转】

    正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个 ...

  8. ORACLE MTTR

    实例恢复时间:指的是将数据文件的最后一个检查点(检查点位置)推进到控制文件中记录的最新SCN 所需的时间.管理员可以通过设置MTTR 目标以及调整重做日志组的大小来控制该时间.MTTR 指导:Mean ...

  9. JAVA NIO 真正做到处理一个事件

    如下图所示: 组1:如果只有上面的红框,不能真正处理该事件,下次执行select()方法,仍然可以select出来该事件.出现死循环现象.如果只有下面的红框,下次select()结果为0,如果外层循环 ...

  10. (三)Android中Intent概念及应用

    一.显示Intent startActivity(new Intent(MainActivity.this,BAty.class)); 显示Intent直接指定要启动的Intent类 注意自己通过创建 ...