LeetCode -- Two Sum
来源:互联网 发布:天行九歌歌词解释知乎 编辑:程序博客网 时间:2024/06/08 00: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
class Solution {public: vector<int> twoSum(vector<int> &numbers, int target) { vector<int> res; map<int, int> m; for(int i = 0; i < numbers.size(); i++){ if(m.count(numbers[i]) == 0) { m[target - numbers[i]] = i + 1; } else { res.push_back(m[numbers[i]]); res.push_back(i + 1); break; } } if(!res.empty()) sort(res.begin(), res.end()); 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
- 【实验报告】《数据结构》实验一: VC编程工具的灵活使用
- Django Models 一对多,多对多,多对一关系解析
- rtx内核上锁与解锁 与 RTX时间管理
- 第01课 OpenGL窗口(2)
- C++内存管理
- LeetCode -- Two Sum
- Android:应用程序签名总结
- 从JVM看性能分析与设计
- Cocos2d3x命令
- Java I/O全文摘要(四)数据源,文件流
- 哈理工OJ1671解题报告
- 解决“MS Visual Database Tools 找不到指定模块”
- 如何在CentOS上安装并汉化Code::Blocks
- struts2 学习二 用户登录处理