LeetCode-Two Sum
来源:互联网 发布:出差携带知乎 编辑:程序博客网 时间:2024/06/09 14:28
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; map<int, int> hMap; for(int i=0; i<numbers.size(); i++) hMap[numbers[i]]=i; for(int j=0; j<numbers.size(); j++){ int a = target - numbers[j]; if(hMap.find(a) != hMap.end() && j != hMap[a]){ res.push_back(j+1); res.push_back(hMap[a] + 1); return res; } } }};
0 0
- 【Leetcode】Two Sum (Sum)
- LeetCode: Two Sum
- LeetCode: Two Sum
- [Leetcode] Two Sum
- LeetCode : Two Sum
- Leetcode: Two Sum
- leetcode 46: Two Sum
- [LeetCode] Two Sum
- LeetCode 1 - Two Sum
- Leetcode : Two Sum
- 【leetcode】Two Sum
- [LeetCode]Two Sum
- leetcode Two Sum
- [Leetcode] Two Sum
- [leetcode] Two Sum
- LeetCode - Two Sum
- leetcode之Two Sum
- LeetCode | Two Sum
- 算法导论
- python3.2 简单面向对象编程
- 编程珠玑
- Linux下g++编译C++连接oracle(OCCI)出现的问题及解决方式
- Debian下配置OpenGL
- LeetCode-Two Sum
- 拓朴图绘制技术
- PHP数据类型隐性转换的陷阱
- Red hat 5.8 挂载裸设备
- 对文件的新认识
- 剑指offer面试题08
- vim打开多窗体、多文件的操作方法
- “鸡蛋和100层楼”问题的深入思考
- C# WinForm 进度条可复用窗口设计