【LeetCode】2. 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
提示:
此题非常常见,解决思路通常可以有两种,一种是使用哈希表,其时间复杂度为O(n),另一种是对数组进行排序,时间复杂度是O(nlogn)。不过实际执行下来是第二种的耗时更少。所以有时候在数据规模不大的时候,系数也是挺关键的一个因素。
代码:
哈希表方法:
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
int n = (int)nums.size();
for (int i = ; i < n; i++) {
auto p = map.find(target-nums[i]);
if (p!=map.end()) {
return {p->second+, i+};
}
map[nums[i]]=i;
}
}
};
排序方法:
class Solution
{
public:
vector<int> twoSum(vector<int>& numbers, int target)
{
vector<int> tmpNumbers(numbers.begin(), numbers.end());
sort(tmpNumbers.begin(), tmpNumbers.end()); int val1 = -;
int val2 = -;
int i = ;
int j = tmpNumbers.size() - ;
while(i < j) {
if(tmpNumbers[i] + tmpNumbers[j] < target) ++i;
else if(tmpNumbers[i] + tmpNumbers[j] > target) --j;
else {
val1 = tmpNumbers[i];
val2 = tmpNumbers[j];
break;
}
} vector<int> result;
for(int i = ; i < numbers.size(); ++i) {
if(numbers[i] == val1 || numbers[i] == val2) result.push_back(i + );
if( == result.size()) return result;
}
return result;
}
};
【LeetCode】2. Two Sum的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】#1 Two Sum
[Question] Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
- 【LeetCode】437. Path Sum III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + DFS BFS + DFS 日期 题目地 ...
随机推荐
- 并发容器之CopyOnWriteArrayList(转载)
Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这 个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改 ...
- HDFS 架构简述
HDFS 架构简述 Hadoop分布式文件系统(HDFS)是一个分布式的文件系统,运行在廉价的硬件上.它与现有的分布式文件系统有很多相似之处.然而与其他的分布式文件系统的差异也是显着的.HDFS是高容 ...
- Java 多线程详解(二)------如何创建进程和线程
Java 多线程详解(一)------概念的引入:http://www.cnblogs.com/ysocean/p/6882988.html 在上一篇博客中,我们已经介绍了并发和并行的区别,以及进程和 ...
- Spring Boot 学习(1)
文 by / 林本托 Tip 做一个终身学习的人. Spring Boot 初体验 Spring Boot 包含了很多 start(Spring boot 中 的叫法,就是一个模块,后文统一称模块,便 ...
- 磁盘分区-gdisk用法
gdisk用法 gdisk - InteractiveGUIDpartitiontable (GPT) manipulator GPTfdisk (akagdisk) isatext-modemenu ...
- springcloud(八):配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- 图解Git命令
上面的四条命令在工作目录.暂存目录(也叫做索引)和仓库之间复制文件. ·git add files把当前文件放入暂存区域. ·git commit 给暂存区域生成快照并提交. ·git reset - ...
- 搭建Elasticsearch集群常见问题
一.ES安装方法: Linux用户登录(bae),我们用的是5.3版本的包.从官网下载: curl -L -O https://artifacts.elastic.co/downloads/elast ...
- Python教程(2.5)——控制台输入
写Python程序时,你可能希望用户与程序有所交互.例如你可能希望用户输入一些信息,这样就可以让程序的扩展性提高. 这一节我们来谈一谈Python的控制台输入. 输入字符串 Python提供一个叫做i ...
- Javascript事件模型(二):Javascript事件的父元素和子元素
DOM事件标准定义了两种事件流,分别是捕获和冒泡.默认情况下,事件使用冒泡事件流,不使用捕获事件流.你可以指定使用捕获事件流,方法是在注册事件时传入useCapture参数,将这个参数设为true. ...