LeetCode | Two Sum
来源:互联网 发布:mac baren烟丝购买 编辑:程序博客网 时间:2024/06/06 14:13
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { int res1 = 0, res2 = 0; multimap<int,int> m; multimap<int,int>::iterator it; multimap<int,int>::iterator it2; vector<int> res; for(int i=0;i<nums.size();i++) { m.insert(make_pair(nums[i],i)); } it2 = m.begin(); for(int i=0;i<nums.size();i++) { if(it2->first > target/2) break; it = m.find(target-it2->first); if(it!=m.end() && it!=it2) { res1 = it->second; res2 = it2->second; break; } it2++; } if(res1 > res2) swap(res1,res2); res.push_back(res1); res.push_back(res2); return res; }};
1 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
- 关于SearchView的使用理解
- Linux操作系统十个注意事项
- Android Studio 百度地图定位、轨迹及所在位置与标注marker的距离
- LeetCode Nim Game
- label标签的二次点击事件
- LeetCode | Two Sum
- Delphi容器类之---TList、TObjectList、TComponentList、TClassList的使用
- 【闲聊】HUAWEI_Code_Craft sdk目录结构
- Oracle 11.2.0.4 expdp 含CLOB字段(basicfile)超级慢
- JVM笔记(二) 垃圾收集器(1)
- 【题解】 [HAOI2006]受欢迎的牛(强连通分量 tarjan)
- hdoj 1875 通畅工程再续 最小生成树
- python学习笔记(6):tuple的基本操作
- 加速matlab运行的三重境界