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 ...
随机推荐
- jQuery.each(object, [callback])方法,用于处理json数组
通用例遍方法,可用于例遍对象和数组. 不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内 ...
- Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- linux之SQL语句简明教程---COUNT
在上一页有提到,COUNT 是函数之一.由于它的使用广泛,我们在这里特别提出来讨论.基本上,COUNT 让我们能够数出在表格中有多少笔资料被选出来.它的语法是: SELECT COUNT(" ...
- OpenStack high-level Functionsenabled
- 通过 Autostereograms 案例学习 OpenGL 和 OpenCL 的互操作性
引言 在过去的十年里, GPU (图形处理单元)已经从特殊硬件(特供)转变成能够在数值计算领域开辟新篇章的高性能计算机设备. 很多算法能够使用拥有巨大的处理能力的GPU来快速运行和处理大数据量.即使在 ...
- 【STL】算法 — partial_sort
partial_sort接受一个middle迭代器,使序列中的middle-first个最小元素以递增顺序排序.置于[first, middle)内.以下是測试代码: #include <ios ...
- 找出诡异的Bug:数据怎么存不进去
带着学生做课程设计.程序一大,课程中做过了小项目,练过了分解动作,一到合起来了,难免还是要乱了分寸.事实上,实战的功夫,就是这样出来的.(课程设计指导视频链接(第36课时,3.18 银行系统开发).课 ...
- web api 文档声明
namespaceHelloWebAPI.Controllers{ usingHelloWebAPI.Models; usingSystem; usingSystem.Coll ...
- 我是如何开发一个连锁企业的信息系统的,NO.1
我是如何开发一个连锁企业的信息系统的,NO.1 连锁企业的信息系统的开发,一半要经历系统分析.系统设计.系统实施.系统评价和系统维护等五个阶段, 而在每个实施阶段中又具体划分出许多阶段性目标和实施步骤 ...
- java实现二叉树的相关操作
import java.util.ArrayDeque; import java.util.Queue; public class CreateTree { /** * @param args */ ...