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 ...
随机推荐
- Go - 字典(map)
字典是一种内置的数据结构,用来保存 键值对 的 无序集合. (1)字典的创建 1) make(map[KeyType] ValueType, initialCapacity) 2) make(map[ ...
- 7.2WebApi2中的全局异常处理
现在在WebApi中还没有一种简单的方式去记录或者处理全局的异常.一些未处理的异常可以处理通过异常过滤器,但有大量的异常是异常过滤器不能处理的.例如: 从控制器的构造函数引发的异常. 从消息处理程序引 ...
- PYTHON 写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者
def a2(arg): if len(arg) > 2: del arg[2:] li = [12,13,14,15] a2(li) print(li)
- json死循环问题
20.JSON死循环问题: 向前台发送的数据: 出现此类问题主要是由于在所传数据中有包含关系,比如ElementGroup中有Element,Element中又有ElementGroup,此时就会出现 ...
- python3简单爬虫
最近在抽空学了一下python,于量就拿爬是练了下手,不得不说python的上手非常简单.在网上找了一下,大都是python2的帖子,于是随手写了个python3的.代码非常简单就不解释了,直接贴代码 ...
- tomcat相关问题
动态资源:需要转换成静态资源后再响应给客户端,例如:jsp.servlet,其他语言的动态资源有:asp.php 静态资源:无需转发即可直接响应给客户端,例如:html.css.javascript ...
- java17
1:登录注册案例(理解) 2:Set集合(理解) (1)Set集合的特点 无序,唯一 (2)HashSet集合(掌握) A:底层数据结构是哈希表(是一个元素为链表的数组) B:哈希表底层依赖两个方法: ...
- .NET 程序集与命名空间
程序集 程序集(assembly)是一个或多个托管模块,以及一些资源文件的逻辑组合. 组成部分 Windows文件首部 CLR文件首部 程序集清单 类型元数据 MSIL(CIL)代码 嵌入资源集 作用 ...
- SQL Server 2008 R2 企业版/开发版/标准版(中英文下载,带序列号)
一. 简体中文 1. SQL Server 2008 R2 Developer (x86, x64, ia64) – DVD (Chinese-Simplified) File Name: cn_sq ...
- ios原声音频播放AVAudioSession 总结
//音频播放/*英译:record:录音 */ 1 导入头文件#import<AVFoundation/AVFoundation.h>//AVAudioSession是一个单例模式.在IO ...