LeetCode_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
参见《编程之美》2.12快速寻找满足条件的两个数。这里采用的解法2中的hash表,采用C++中的map容器,以数组元素为key,因为要返回下标,以下标为value。算法时间复杂度O(n),空间复杂度O(n).
代码(AC):
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int,int> map1;//<数值,下标>
vector<int> vin;
int len = numbers.size();
//cout<<len<<endl;
for(int i = 0;i<len;i++)
{
map1[numbers[i]] = i;
}
for(int i = 0;i<len;i++)
{
int sum = target - numbers[i];
map<int,int>::iterator it = map1.end();
if(it != map1.find(sum)&&map1.find(sum)->second!=i)
{
vin.push_back(i+1);
vin.push_back(map1.find(sum)->second+1);
}
}
return vin;
}
};
如果不要求返回符合条件元素在原数组的下标,《编程之美》中的解法三很巧妙。如果已经了这个数组任意两个元素之和的有序数组,那么采取Binary Search,在O(logN)时间内就可以解决问题。但是计算每两个元素和的有序数组需要O(n^2),我们并不需要求解这个数组。这个思考启发我们,可以直接对两个数字的和进行有序遍历,降低时间复杂度。
首选,对原数组进行排序,采用Qsort,时间复杂度O(NlogN);
声明两个下标,i = 0;j = n-1;比较array[i]+array[j]是否等于target,如果是返回array[i]和array[j],若小于target,则i++;如果大于target,则
j--;遍历一次即可。时间复杂度O(n).所以该方法的时间复杂度是O(n)+O(NlogN) = O(NlogN)。要是题目返回原始数组的下标,则在对原数组排序的时候要保存各个元素在原数组的下标。
伪代码:
Qsort(array);
for (i=0,j=n-1;i<j)
if(array[i]+array[j]==target) return (array[i],array[j]);
else if(array[i]+array[j]<target) i++;
else j--;
return ;
LeetCode_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 ...
随机推荐
- bash之局部变量与子shell(转载)
shell是每个接触linux.unix用户不得不会的工具,谈到shell就又联系到bash,因为这个shell是普遍被使用的.那么bash中的局部变量和子shell你是否能熟练掌握呢?这里推荐一本学 ...
- 介绍编译的less的几种IDE工具
介绍编译的less的两种IDE工具 现在css预编译越来越普及了,著名的有less.sass.stylus等等等等.功能上基本上都是大同小异.这些个玩意儿主要表达的意思就是:“像编程一样的编写你的cs ...
- hdu6153 扩展kmp求一个字符串的后缀在另一个字符串出现的次数。
/** 题目:hdu6153 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给定两个串,求其中一个串t的每个后缀在另一个串s中出现的次数乘以 ...
- selenuim爬虫实战 (下)
SuperLOFTERDownloader7.java package test; import java.io.IOException; import java.util.ArrayList; im ...
- 10个 jQuery 小技巧
10个 jQuery 小技巧 -----整理by: xiaoshuai 1. 返回顶部按钮 可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. // Bac ...
- 使用JAVASCRIPT进行数据完整性验证
页面输入完整性是编写BS经常遇到的问题,如果那里需要就到那里写,那可是要花不少的时候,并且造成不必要的浪费,下面是一个通过校验脚本,使用非常方便,通过传入FORM名就可以进行校验,通过在页面控件中增加 ...
- 使用css全面美化input标签
做网站时经常有这样那样的需要,要美化input ,于是CSS的美化必不可少.和程序人生的站长交流,他发给我这个. 下面是CSS样式 input { border:1px solid #B3D6EF; ...
- C++ 类的对象管理模型初讲
//类的对象管理模型初讲 #include<iostream> using namespace std; class PointA{ private: int x;//占据4个字节大小的内 ...
- Web设计的速查卡(转)
一.排版 1.VisiBone Font Card Download: GIF 2.常用字体表 (HTML) 3.混合字体 (PDF) 二.单元/尺寸 1.Points 和Pixels近似转换表 (H ...
- 【BZOJ】1643: [Usaco2007 Oct]Bessie's Secret Pasture 贝茜的秘密草坪(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1643 这题和完全背包十分相似, 但是不能用1维做........原因貌似是不能确定块数(还是有0的面 ...