【题目】

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. nginx grok 正则错误的输出情况

    nginx 配置: http { include mime.types; default_type application/octet-stream; log_format main '$http_h ...

  2. cf467A George and Accommodation

    A. George and Accommodation time limit per test 1 second memory limit per test 256 megabytes input s ...

  3. sriov查看pf-vf对应关系

    自己写的, 方便调试. $ cat pf-vf echo "physfn is $1"echo "pf info:"ls /sys/class/net/$1 - ...

  4. STL set接口中使用结构体类型

    需要在结构体中重载'<'运算符,下面是我写的一个例子: #include<iostream> #include<set> using namespace std; str ...

  5. Elasticsearch的javaAPI之percolator

    Elasticsearch的javaAPI之percolator percolator同意一个在index中注冊queries,然后发送包括doc的请求,返回得到在index中注冊过的而且匹配doc的 ...

  6. ThreadLocal 在web环境下使用的边界问题

    ThreadLocal 相关分析,请查看http://wangxinchun.iteye.com/blog/1884228 另外一个必须要提的点是: ThreadLocal在线程池环境下的使用. 比如 ...

  7. C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库

    第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...

  8. JS函数的四种调用模式

    函数在js中具有四种身份,分别为函数.方法.构造函数.apply或call调用 函数调用    函数调用模式中this指全局对象(window) var f1 = function() { alert ...

  9. UVA 1610 Party Games

    题意: 给出一系列字符串,构造出一个字符串大于等于其中的一半,小于另一半. 分析: 取大小为中间的两个a,b(a<b).实际上就是找出第一个小于b的同时大于等于a的字符串,直接构造即可. 代码: ...

  10. ubuntu下nvm,node以及npm的安装与使用

    一:安装nvm 首先下载nvm,这里我们需要使用git,如果没有安装git,可以使用 sudo apt-get install git 来安装 git clone https://github.com ...