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)使用Hash,存储各个值出现的下表,O(N);

2)使用排序,O(NlogN)。

c++,O(N):

class Solution {
public: vector<int> twoSum(vector<int> &numbers, int target) {
vector<int> v;
map<int,int> m;
for(int i=0;i<numbers.size();i++){
if(m.find(target-numbers[i])!=m.end()){
int i1=i+1;//
int i2=m.find(target-numbers[i])->second;
v.push_back(min(i1,i2));
v.push_back(max(i1,i2));
return v;
}
m[numbers[i]]=i+1;//序号从1开始
}
}
};

java: O(NlogN)

public class Solution {
class Node{
int pos;
int val;
};
public int[] twoSum(int[] numbers, int target) {
Node[] nodes = new Node[numbers.length];
for(int i=0;i<numbers.length;i++){
nodes[i] = new Node();
nodes[i].pos=i+1;
nodes[i].val=numbers[i];
}
Arrays.sort(nodes, new Comparator<Node>(){
public int compare(Node n1,Node n2){
return n1.val-n2.val;
}
});
int[] r = new int[2];
int i=0;
int j=nodes.length-1;
while(i<j){
int sum =nodes[i].val+nodes[j].val;
if(sum==target){
int max=Math.max(nodes[i].pos,nodes[j].pos);
int min=Math.min(nodes[i].pos,nodes[j].pos);
r[0]=min;
r[1]=max;
break;
}else if(sum<target){
i++;
}else{
j--;
}
}
return r;
}
}

Two Sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. “基础提供程序在Open上失败”

    本来布置在IP为[x.x.x.x]的WCF服务好好的,但是今天突然就有问题了,一调用报错"基础提供程序在Open上失败"... 服务器上的有问题,先试试本地的服务能不能用吧,连的都 ...

  2. Response.End()出现ThreadAbortException 异常

    A. 如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.异常内容:由于代码已经过 ...

  3. 解决table不能换行的问题与CSS之自动换行总结

    table不能换行问题 一般是:一行里面全是数字或是字母或者结尾有多个感叹号而导致 table不能换行,中文默认的会自动换行的,字母不能换行问题:style="table-layout:fi ...

  4. redis该如何分区-译文(原创)

    写在最前,最近一直在研究redis的使用,包括redis应用场景.性能优化.可行性.这是看到redis官网中一个链接,主要是讲解redis数据分区的,既然是官方推荐的,那我就翻译一下,与大家共享. P ...

  5. javascript基础01

    javascript基础01 Javascript能做些什么? 给予页面灵魂,让页面可以动起来,包括动态的数据,动态的标签,动态的样式等等. 如实现到轮播图.拖拽.放大镜等,而动态的数据就好比不像没有 ...

  6. 如何让自己的app尽量不被系统杀死

    1. 在Service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override public int onStartComman ...

  7. C#高级编程笔记 Day 6, 2016年9月 14日 (泛型)

    1.泛型类的功能:在创建泛型类时,还需要一些其他C#关键字.例如,不能把null赋予泛型类型.此时,可以使用default 关键字.如果泛型类型不需要Object类的功能,但需要调用泛型类上的某特定方 ...

  8. Qt StyleSheet皮肤css源码

    使用方式如下 //设置皮肤样式 static void SetStyle(const QString &styleName) { QFile file(QString(":/imag ...

  9. Spring @AspectJ 实现AOP 入门例子(转)

    AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子. 引用一个猴子偷桃,守护者守护果园抓住猴子的小情节. 1.猴子偷桃类(普通类): package com.samter.common ...

  10. Bootstrap3 CSS样式基本用法总结

    按钮 a,input,button都可以设置为按钮 a标签按钮   button标签按钮 <a class="btn btn-default" href="#&qu ...