LintCode-56.两数之和
两数之和
给一个整数数组,找到两个数使得他们的和等于一个给定的数 target。
你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标。注意这里下标的范围是 1 到 n,不是以 0 开头。注意事项
你可以假设只有一组答案。
样例
给出 numbers = [2, 7, 11, 15], target = 9, 返回 [1, 2].
挑战
Either of the following solutions are acceptable:
- O(n) Space, O(nlogn) Time
- O(n) Space, O(n) Time
标签
排序 哈希表 爱彼迎 数组 脸书 两根指针
方法一:暴力破解
时间复杂度O(n^2)
code
class Solution {
public:
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1+1, index2+1] (index1 < index2)
*/
vector<int> twoSum(vector<int> &nums, int target) {
// write your code here
if(nums.empty()) {
return vector<int>();
}
int len = nums.size();
vector<int> ret;
for(int i=0; i<len; ++i) {
for(int j=i+1; j<len; ++j) {
if(nums[i] + nums[j] == target) {
ret.push_back(i+1);
ret.push_back(j+1);
return ret;
}
}
}
return vector<int>();
}
};
方法二:利用hash
时间复杂度O(n),空间复杂度O(n)
code
class Solution {
public:
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1+1, index2+1] (index1 < index2)
*/
vector<int> twoSum(vector<int> &nums, int target) {
// write your code here
int size = nums.size();
int i = 0;
vector<int> result;
if(size <= 0) {
return result;
}
map<int, int> hashMap;
for(i=0; i<size; i++) {
hashMap.insert(pair<int, int>(nums[i], i));
}
for(i=0; i<size; i++) {
int temp = target - nums[i];
if (hashMap.count(temp)!=0 && hashMap[temp]!=i) {
result.push_back(i+1);
result.push_back(hashMap[temp]+1);
break;
}
}
if(result[0] > result[1]) {
int temp = result[0];
result[0] = result[1];
result[1] = temp;
}
return result;
}
};
LintCode-56.两数之和的更多相关文章
- [Lintcode two-sum]两数之和(python,双指针)
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个 ...
- 56.两数之和.md
描述 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 0 到 n-1. ...
- [LintCode/LeetCode]——两数和、三数和、四数和
LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号: ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- LeetCode 371. Sum of Two Integers (两数之和)
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...
- LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
随机推荐
- windows下MySQL免安装版配置教程mysql-8.0.12-winx64.zip版本
引用1:https://blog.csdn.net/weixin_42831477/article/details/81589325 引用2:https://blog.csdn.net/qq_3193 ...
- 阿里云Windows远程连接出现身份验证错误,要求的函数不正确”的报错。
最近很多阿里云用户在远程Windows Server的云服务器ECS时出现“身份验证错误,要求的函数不受支持”的报错. 这个问题解决起来非常简单,修改组策略中的一个配置就可以了. 在运行中输入gped ...
- Hive操作之向分区表中导入数据的语义错误
1.建完分区表之后,向表中导入数据 命令为: load data local inpath '/home/admin/Desktop/2015082818' into table db_web_dat ...
- 优龙FS2410开发板学习过程遇到问题总结
以下的问题及其解决办法是基于优龙FS2410开发板,不定期更新 ============================================================= 开发学习环境 ...
- java static代码块执行时机
之前一直认为static块是在class load的时候执行,今天在验证Spring初始化Context loader的时候,发现bean的static块并没有执行. Java代码: 1 Class ...
- 北京Uber优步司机奖励政策(12月28日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Farpoint使用一点小总结
Farpoint表格编辑的功能是非常强大的,记录下自己常用到的地方. 使用的版本:FarPoint.Win.Spread.5.0 1.Farpoint 设置为不可编辑状态 this.FPProxyIt ...
- iOS 测试工具reveal可视化调试工具的使用
简单翻译一下reveal可视化图形工具插入项目的官方文档(官方英文版file:///Applications/Reveal.app/Contents/SharedSupport/Documentati ...
- CentOS 7.2安装11g Grid Infrastructure
Preface Oracle claimed that 11g RAC is supported on Redhat Linux 7 and above version,but the ...
- TW实习日记:第26天
这周组长休年假去了,并且之前主要负责的项目也已经上线了,可以说没那么忙了,手头就一个协助别的组做的移动端项目.可是这个项目特别坑,由于网端是9年前的项目,导致后台的接口有非常多的问题,并且入参多得令人 ...