two-sum
来源:互联网 发布:mahout java实例教程 编辑:程序博客网 时间:2024/04/24 01:45
题目描述
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
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int> res; unordered_map<int,int> my_map; int bef; int aft; my_map[numbers[0]]=0; for(int i=1;i!=numbers.size();++i) { if(my_map.find(target-numbers[i])!=my_map.end()) { bef=my_map[target-numbers[i]]; aft=i; } else my_map[numbers[i]]=i; } res.push_back(bef+1); res.push_back(aft+1); return res; }};
0 0
- Two Sum
- Two Sum
- Two Sum
- two sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- Two Sum
- 模板_matlab 匈牙利算法(最大匹配数/最小覆盖点)
- oracle数据库基于goldengate的双机热备配置手册
- ubuntu16.04安装bvlc/caffe
- linux 下 ifcfg-eth0 配置
- Sql 时间戳(int)转换为datetime
- two-sum
- HDU 1233:还是畅通工程
- Android Studio学习
- Intel HAXM is required to run this AVD 问题的解决
- Mysql中的数据分组和子查询
- 要求完成一个接口ShowMessage,TV类调用接口“I am TV”。
- Python3环境安装PySpider爬虫框架过程
- 利用HttpClient进行post请求
- 扩展欧几里得定理