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 ...
随机推荐
- 【LeetCode练习题】Merge Sorted Array
Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note ...
- android xUtils的使用
gethub地址:https://github.com/wyouflf/xUtils/ xUtils简介 xUtils 包含了很多实用的android工具. xUtils 支持大文件上传,更全面的ht ...
- oracle表空间创建及管理
一.数据文件和数据库逻辑存储结构: 一个表空间包含一个或多个数据文件,一个表空间包含一个或多个段,一个段包含一个或多个区,一个区包含一个或多个连续的数据库块,一个数据库块包含一个或多个操作系统块.段是 ...
- .net 链接ORACLE的安装包
odp.net.ma
- 用CSS做圆角矩形
第一种方法:如果是CSS2.2的话,可以简单写一个制作圆角矩形,分上中下裁成三张图 <title>CSS3实现圆角</title> <style type="t ...
- Spring事务传播机制详解
1 事务的传播属性(Propagation) 1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none ...
- 简述UITextField的属性和用法
0. enablesReturnKeyAutomatically 默认为No,如果设置为Yes,文本框中没有输入任何字符的话,右下角的返回按钮是disabled的. 1.borderStyle ...
- C期未考试参考答案
输入10个数,要求编写一个排序函数,能够实现按绝对值从大到小排序.在主函数中输入10个数.输出排序后的10个数 #include<stdio.h>#include<math.h> ...
- nginx 学习笔记【持续更新...】
1.如果在安装过程中出现以下错误 需要安装pcre库 解决方案:yum install pcre 2.如果nginx启动提示端口被占用,则停止该端口的服务再启动nginx,一般为httpd服务 解决方 ...
- DataTable中执行DataTable.Select("条件"),
我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...