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. 用C#开发ActiveX控件,并使用web调用

    入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己.最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是winform才能使用了,但是有需求咱们必须得完成啊,所以涉及 ...

  2. IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的

    IIS连接数 一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,这边先从普通不懂代码用户角度理解IIS连接数 顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫" ...

  3. Mac中安装Vim7.4

    Mac上的Vim Mac本身其实是预装了Vim的,但是目前的系统中都是Vim7.3版本的,而最新的Vim已经是7.4版了,因此为了能够使用最新版的vim,必须要对Mac中的vim要么升级,要么重装.在 ...

  4. 网站建设中帝国cms如何循环调用栏目下级分类

    类似的形式,调用下级分类 ?php $bclassid=[!--self.classid--]; //选择当前栏目的id,如果调用指定栏目下的多级分类,则填写栏目id //取得本栏目下的子栏目 ? [ ...

  5. 周末娱乐一下--------恶搞windows小脚本

    下面这是个循环DOS命令,使用了C中的goto语句 echo命令式输出命令 set命令是设置命令 var是变量,初始为0 :continue是一个用于goto的标示. %var%输出变量名,%var% ...

  6. BZOJ 1060: [ZJOI2007]时态同步

    Description 一个有根树,你只能进行增加操作,问你将所有叶节点到根的路径权值相同至少需要增加几次. Sol 我也不知道该叫什么算法... 反正就是记录一下到子节点到当前节点的最大距离统计答案 ...

  7. 精简的网站reset 和 css通用样式库

    参考链接:http://www.zhangxinxu.com/wordpress/2010/07/我是如何对网站css进行架构的/ reset.css body{ line-height:1.4; c ...

  8. Linq查询表达式

    目录 1. 概述 2. from子句 3. where子句 4. select子句 5. group子句 6. into子句 7. 排序子句 8. let子句 9. join子句 10. 小结 1. ...

  9. Eclipse关闭XML文件验证的方法

    XML的编写是否符合规范,可以通过XML Schema或DTD进行验证,但有时候电脑本来就很卡,而且XML的某些错误并未导致程序无法运行的情况下,暂时关闭XML的验证也算不错的选择. 如web.xml ...

  10. Linux pthread

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h& ...