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. MRDS学习三——机械车的改良(Activity的介绍)

    Activity:VPL中化繁为简的工具,可以使得真个VPL容易让人看懂,也能跟好的控制.它就很像把一堆比较复杂但相关的流程组合成一个自定义的Activity. 完成Activity的定义: 第一步: ...

  2. iOS开发

    #import 预处理指令,相对于 #include 而言,能防止重复拷贝,它可以导入OC头文件,也可以导入C头文件. OC中在一个框架中,有一个主头文件(该头文件名称一般跟框架名称相同),该主头文件 ...

  3. Linux文件(区域)锁函数 -- open()、fcntl()

    一.什么是文件锁定 对于锁这个字,大家一定不会陌生,因为我们生活中就存在着大量的锁,它们各个方面发挥着它的作用,现在世界中的锁的功能都可归结为一句话,就是阻止某些人做某些事,例如,门锁就是阻止除了屋主 ...

  4. CSS修改input[type=range]滑块样式

    input[type="range"]是html5中的input标签新属性,样子如下: <input type="range" value="4 ...

  5. C段渗透攻击必看的技术知识

    假设想攻击的主机IP是:61.139.1.79 同一子网下我们已有权限的主机IP是:61.139.1.88并可以3389登陆   第一步: tracert 61.139.1.1   C:\WIN200 ...

  6. Mac下Boost环境搭建

    Boost,一个功能强大.跨平台.开源而且免费的C++程序库,可以在其官网了解更多:http://www.boost.org,C++标准经过不断的升级完善,现在已经功能越来越吸引人了,Boost开发过 ...

  7. UVA 10780 Again Prime No Time.(数学)

    给定两个整数m和n,求最大的k使得m^k是n!的约数 对m质因子分解,然后使用勒让德定理求得n!包含的质数p的阶数,min(b[i] / a[i])即为结果k, 若为0无解 #include<c ...

  8. html标签思维导图

  9. Spring整合web开发

    正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet { public void doGet(HttpServlet ...

  10. linux磁盘读写性能优化

    在LINUX系统中,如果有大量读请求,默认的请求队列或许应付不过来,我们可以 动态调整请求队列数来提高效率,默认的请求队列数存放在/sys/block/xvda/queue/nr_requests 文 ...