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 ...
随机推荐
- java getter和setter的方法及内部类的调用
class Test{ public static void main(String[]args){ Person person=new Person(); person.age=22; person ...
- c语言:矩阵相乘-矩阵相加 新手练习1
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> voi ...
- 中国大学MOOC-C程序设计(浙大翁恺)—— 时间换算
时间换算(10分) 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小 ...
- CF 914 D. Bash and a Tough Math Puzzle
D. Bash and a Tough Math Puzzle http://codeforces.com/contest/914/problem/D 题意: 单点修改,每次询问一段l~r区间能否去掉 ...
- focus如何实现事件委托
事件委托是利用事件冒泡机制的一种优化手段,如果有很多列表元素要绑定事件,那么就可以用事件委托来优化(不需要给每个元素都绑定事件).但是对于focus这种特殊的表单事件,它不会冒泡,那么又该如何实现这一 ...
- 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...
- CSS选择器语法&示例
CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) ...
- 第三十九篇 Python异常处理
一. 什么是异常 异常就是程序运行时发生的错误,在程序出现错误时,则会产生一个异常,若程序没有处理它,则会抛出该异常,程序的运行也随之终止,在python中,错误触发的异常如下 错误分成两种: #语法 ...
- 跟浩哥学自动化测试Selenium -- 我的第一个Demo (2)
我的第一个Demo 开始写第一个 Demo 之前,先熟悉一下编写 Selenium 脚本的四个步骤: 驱动路径写法分析:System.setProperty 主要做用是设置系统属性,第一个参数为系统属 ...
- C++0x,std::move和std::forward解析
1.std::move 1.1std::move是如何定义的 template<typename _Tp> constexpr typename std::remove_reference ...