[LeetCode 题解]: Two Sum
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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. 题意
给定一组数字,找到两个元素,使得两者的和满足一个给定的目标值Target。
返回值要求:输出的两个数,index1 必须小于index2.
3. 思路
1. 暴力BF,直接超时
2. hash,用映射 STL: map + vector
4. 解法
class Solution {
public:
vector<int> twoSum(vector<int> &numbers, int target) {
map<int ,int> match;
vector<int> ans;
ans.clear();
int i;
for(i=0;i<numbers.size();i++)
match[numbers[i]] = i+1; // 记录位置
for(i=0;i<numbers.size();i++)
{
if(match.find(target - numbers[i]) != match.end() && i+1 != match[target - numbers[i]]) // map映射
{
ans.push_back(i+1);
ans.push_back(match[target-numbers[i]]);
break;
}
}
return ans;
}
};
![]() |
作者:Double_Win 出处: http://www.cnblogs.com/double-win/p/3751836.html 声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~ |
[LeetCode 题解]: Two Sum的更多相关文章
- [LeetCode 题解] Combination Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a se ...
- LeetCode题解——Two Sum
题目地址:https://oj.leetcode.com/problems/two-sum/ Two Sum Given an array of integers, find two numbers ...
- LeetCode题解之Sum Root to Leaf Numbers
1.题目描述 2.问题分析 记录所有路径上的值,然后转换为int求和. 3.代码 vector<string> s; int sumNumbers(TreeNode* root) { tr ...
- 回溯法 leetcode题解 Combination Sum 递归法
题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...
- LeetCode题解之 Sum of Left Leaves
1.题目描述 2.问题分析 对于每个节点,如果其左子节点是叶子,则加上它的值,如果不是,递归,再对右子节点递归即可. 3.代码 int sumOfLeftLeaves(TreeNode* root) ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
随机推荐
- mongodb Java(八)
package com.mongodb.text; import java.net.UnknownHostException; import com.mongodb.DB; import com.mo ...
- grideh SelectedRows Bookmark
VCL grideh 选中多行 TBookmark.Bookmark.GotoBookmark TBookmark bm= DataSet->GetBookmark(); DataSet-> ...
- Spring Boot实践——SpringMVC视图解析
一.注解说明 在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢? 首先,@EnableWebMvc=Web ...
- Java后端发送email实现
依赖的jar包 <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail& ...
- windows共享连接显示无法打开
Ping目标地址和名称可以连通,但是访问告知无法打开或找到名称,看凭据设置正常,重启无效. 判断:可能是由于凭据过期引起,更新凭据,重启,仍旧无效. 修改IP地址进行访问,成功打开. 清理网络连接状态 ...
- UVALIVE 4556 The Next Permutation
4556 The Next PermutationFor this problem, you will write a program that takes a (possibly long) str ...
- PHP 缓存插件之 Zend Opcache ( 取代 APC )
简介: Zend Opcache .APC 这都是 PHP 代码加速器,将 PHP 源代码的编译结果缓存起来,再次调用时对比时间标记,如果没有改变则使用缓存数据,免去再次解析代码的开销. APC 止步 ...
- Python修饰器讲解
转自:http://www.cnblogs.com/rollenholt/archive/2012/05/02/2479833.html 文章先由stackoverflow上面的一个问题引起吧,如果使 ...
- docker plugin test
docker build -t docker-volume-drbd . id=$(docker create docker-volume-drbd true) docker export $id - ...
- Cassandra修改集群名称
如果需要在不影响存储数据的情况下,更改cassandra集群名字,可采用如下步骤: 1. 对集群所有节点(for each node)依次连接CQLSH,使用如下命令: UPDATE system. ...
