leetcode Two Sum 哈希表
来源:互联网 发布:lol徐老师淘宝店多少 编辑:程序博客网 时间:2024/05/16 19: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
刚开始直接两层for循环超时,哈哈,还是要用哈希表。
class Solution {public:vector<int> twoSum(vector<int>& nums, int target) {vector<int>res;if (!nums.size()) return res;map<int, int>Hash;for (int i = 0; i < nums.size(); i++){if (Hash.find(target - nums[i]) != Hash.end()){res.push_back(Hash[target - nums[i]]+1);res.push_back(i+1);return res;}Hash[nums[i]] = i;}return res;}};
0 0
- leetcode Two Sum 哈希表
- 【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
- 猴子向右看,FOJ上某题 单调栈
- [转]短信验证码如何防止不恶意点击被刷!
- Cannot run program "XXXXX/android-sdks/build-tools/aapt.exe": error=2,
- TP接口token
- MYSQL启动后报:ERROR! The server quit without updating PID file错误的问题解决
- leetcode Two Sum 哈希表
- VS2010+QT5编程之巧妙运用定时器解决QTreeView/QTableView等QAbstractItemView实时条目统计问题
- OpenMAX/IL: OMX IL 学习笔记【1】- 接口与头文件
- Lex 和 Yacc 编程环境配置(Parser Generator2 + VC6.0 配置)
- Spring中Hibernate配置笔记
- Hibernate学习总结
- plsql中异常的传递
- logistic regression 最基础的分类算法
- C++实现字符串之模式匹配(一)