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 ...
随机推荐
- 映射 SQL 和 Java 类型
http://alex2009.blog.51cto.com/749524/272942 AJAX: http://www.w3school.com.cn/jquery/ajax_ajax.asp h ...
- mysql 匹配update
update 语句示例: UPDATE `zjzc`.`QRTZ_SCHEDuler_state` SET `ip`='220.191.34.246' WHERE `sn`='3117764' and ...
- poj 1664 放苹果_整数拆分
题意:略 用手机敲的代码真是泪流满面,终于过了 #include<iostream> using namespace std; int dp[110][110]; void solve() ...
- hunnu11543:小明的烦恼——分糖果
Problem description 小明在班里一直是个非常公正的孩子.这点同学和老师都非常清楚,这不,老师每周都会从家里带来一些糖果.然后叫小明把糖果分给其它小朋友,但这个班里的同学都有一个非 ...
- Android的Bitmap和BitmapDrawable类解析-android学习之旅(六十)
使用简单图片 使用Drawable对象 bitmap和BitmapDrawable对象 package peng.liu.test; import android.app.Activity; impo ...
- mysql 主从不同步处理--数据库初始化
问题处理借鉴至网上的内容 又一次做主从,全然同步 在主库新建一张表后.在slave 段发现数据没有同步过去. mysql version:5.6.10 os :rhel 5.6 解决过程例如以下: 1 ...
- Fault Diagnosability Infrastructure Overview
Fault Diagnosability Infrastructure Overview The fault diagnosability infrastructure aids in prevent ...
- 《think in python》学习-10
think in python 10 列表 和字符串相似,列表是值得序列.在列表中,它可以是任何类型,列表中的值成为元素,有时也称为列表项 s = [10,20,30,40] print s #列表也 ...
- javascript设计模式——Observer
定义一种一对多的从属关系,当一个目标状态改变,所有他的从属对对象都将收到通知. 1.简单的Observer模式 实现 var Observer = function(){ this.list = [] ...
- C# AES,AesManaged使用学习
加密 static byte[] EncryptBytes_Aes(byte[] plainText, byte[] Key, byte[] IV) { // Check arguments. ) t ...