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)使用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的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- util类中非静态方法中注入serivce,在controller层是使用util。
今天碰到如题的问题,刚一开始在util中注入service总是注入失败,起初我以为是util中没有注入成功,debug看了一下果然注入不进来. 然后各种纠结,最终坑爹的问题是在controller直接 ...
- 如何自定义iphone个性铃音
准备工作:itunes.(Netease Cloud Music).iphone 1.下载你想要的铃音原音乐: 2.打开itunes,向音乐库中添加刚刚下载的音乐: "文件"-&g ...
- codevs1540 银河英雄传说
描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集 ...
- MSYS2的源配置
关于MSYS2的文章可以参考下面的链接,笔者不多赘述: msys2安装笔记 MSYS2 + MinGW-w64 + Git + gVim 环境配置 msys2环境搭建 msys2安装g++: pacm ...
- PYTHON 写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者
def a2(arg): if len(arg) > 2: del arg[2:] li = [12,13,14,15] a2(li) print(li)
- this a a mark down test
*** this is title ''' code '''
- Git教程推荐
推荐:廖雪峰的官方网站-Git教程,面向初学者,浅显易懂.
- webservice发布服务:CXF及客户端调用
2.CXF:(与spring整合) CXF相对来说操作没有AXIS繁琐 1.导入spring的jar包和cxf的jar包 2.在spring的核心配置文件中配置发布的接口类 <?xml vers ...
- 学习android推荐网站
1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...
- AngularJS SQL 获取数据
使用PHP从MySQL中获取数据: <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...