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 ...
随机推荐
- 原生js实现简单轮播的淡入淡出效果
实现这种淡入淡出的轮播关键在于css的一种设置 首先它不能像传统的轮播显示隐藏 或者左右浮动 他应该让其固定定位使其重叠在一起 达到这种效果 然后设置c3动画属性 transition:1s; ...
- yii学习笔记(6),连接数据库,创建活动记录类
创建数据库用于测试 配置数据库连接 打开yii的配置文件目录下的数据库配置文件config/db.php <?php return [ 'class' => 'yii\db\Connect ...
- 集合之HashMap、Hashtable
HashMap 基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtabl ...
- mongodb安装 超级管理 普通用户
安装MongoDB #1.配置mongo的yum源sudo vi /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4]name=MongoDB ...
- scala 实现日期运算
在scala程序中,有时我们需要对日期进行运算,比如一天之前,两天之前,一个月之前等等,本博文给出了简单的实现方式 val cal = Calendar.getInstance cal.add(Cal ...
- 最新版的stm32f1xx.h文件中取消了u8, u16, u32的类型定义
使用芯片stm32f103zet6和stm32l151c8t6,在移植程序时发现,编译器提示u8未定义: 在Keil MDK 开发环境里,st定义无符号32位整形数据有很多种表示方法: 1 unsig ...
- 【Android】Android Studio真机调试的问题统整
真机调试需要注意以下几个问题 [1]手机的USB调试需开启 [2]手机不能是仅充电模式,需要传输数据模式 [3]有些USB线会偷工减料,请拿一条没问题的线,例如买手机时原厂给的配线 [4]在PC端需要 ...
- 『Linux基础 - 2 』操作系统,Linux背景知识和Ubuntu操作系统安装
这篇笔记记录了以下几个知识点: 1.目前常见的操作系统及分类,虚拟机 2.Linux操作系统背景知识,Windows和Linux两个操作系统的对比 3.在虚拟机中安装Ubuntu系统的详细步骤 OS( ...
- python兵器谱之re模块与正则表达式
一.正则表达式 ·1.正则表达式的应用场景: 应用特有的规则,给我需要的符合规则的字符串,在字符串中只有符合条件的才会被匹配和从大段的字符串中提取需要的数据 ·匹配字符串的规则: ·1.字符串:用户输 ...
- P1488 肥猫的游戏
题目描述 野猫与胖子,合起来简称肥猫,是一个班的同学,他们也都是数学高手,所以经常在一起讨论数学问题也就不足为奇了.一次,野猫遇到了一道有趣的几何游戏题目,便拿给胖子看.游戏要求在一个有n个顶点凸多边 ...