LeetCode-001 Two Sum
【题目】
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的更多相关文章
- LeetCode #001# Two Sum(js描述)
索引 思路1:暴力搜索 思路2:聪明一点的搜索 思路3:利用HashMap巧解 问题描述:https://leetcode.com/problems/two-sum/ 思路1:暴力搜索 一个很自然的想 ...
- 【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 ...
- [Leetcode][001] Two Sum (Java)
题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- 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 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
随机推荐
- 时间TDateTime相当于是Double,即双精度数64位,终于查到它用11位表示e,53位表示精度(整数小数一起),最前面一位表示正负
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Internal_Data_Formats 关于Double的RTL函数,好像就一个:TrySt ...
- HDU2114 Calculate S(n) (取模算术)
Calculate S(n) Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- C++关联容器综合应用:TextQuery小程序
本文介绍C++关联容器综合应用:TextQuery小程序(源自C++ Primer). 关于关联容器的概念及介绍,请参考园子里这篇博文:http://www.cnblogs.com/cy568sear ...
- POJ 3368 RMQ-ST
一直感觉RMQ水,没自己写过,今天写了一道题,算是完全独立写的,这感觉好久没有了... 一直以来,都是为了亚洲赛学算法,出现了几个问题: 1.学的其实只是怎么用算法,对算法的正确性没有好好理解,或者说 ...
- 从ora10g 刷数据到 8I基本操作步骤
从ora10g 刷数据到 8I基本操作步骤 master :oracle 10g snapshot site: oralce 8i 在oracle 8i 中物化视图称为快照,oracle 8i建快照的 ...
- Mac 安装Qt5.1编译出现的错误解决
错误提示: :-1: 错误:Xcode is not installed in /Volumes/Xcode/Xcode.app/Contents/Developer. Please use xcod ...
- iOS 四种延时的方法
- (void)initBlock{ //延时的方法 //1:GCD延时 此方式在能够在參数中选择运行的线程. 是一种非堵塞的运行方式,没有找到取消运行的方法. double ...
- string.Format组合跳转路径
string url = this.ResolveClientUrl("~/page/bn_stu.aspx"); string str = string.Format(" ...
- Java基础笔记-String类
String 类(被final修饰) 字符串是一种特殊的对象,一旦字符串被初始化就不可以被改变了.(内容不变) 例如: String s = “abc”; String s1 = new Stri ...
- servlet+ajax+json字符串后台传入,前端解析并把数据循环填入表格实例
写在前面:1.源代码来源于博客http://blog.sina.com.cn/u/2904067371 ,在此基础上对于前端代码稍作更改,把传过来的数据解析并传入表格2.json解析,用eval()3 ...