two sum
来源:互联网 发布:数据科学与r语言 编辑:程序博客网 时间:2024/05/01 04:56
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
解题思路:哈希
#include<iostream>#include<vector>#include<map>using namespace std;vector<int> twoSum(vector<int> &numbers, int target) {vector<int>ResultTwoSum;map<int, int>NumberIdxMap;for (int i = 0; i != numbers.size();++i){auto iter = NumberIdxMap.find(target - numbers[i]);if (iter!=NumberIdxMap.end())NumberIdxMap.insert(make_pair(numbers[i], i));else{ResultTwoSum.push_back(i);ResultTwoSum.push_back(iter->second);break;}}return ResultTwoSum;}
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
- 刷题计划
- 服务器IP分配以及路由表配置
- 使用Python正则表达式RE从CSDN博客源代码中匹配出博客信息
- Devexpress gridcontrol checkbox不能多选问题
- 注释代码快捷键:
- two sum
- 北京治疗老年癫痫病医院-【癫痫专科医院】
- poj 1787 记录路径的多重背包
- C++嵌套类的使用
- 录像脑电图费用_名医养生-淮安特色技术专题
- java学习过程中的异常处理方式
- 怎么解决癫痫病人的心理问题
- mysql事务处理(transcation)
- 打印1到最大的N位数