LeetCode:Two Sum
2016-03-12 12:15
411 查看
Two Sum
Total Accepted: 199288 TotalSubmissions: 913621 Difficulty: Easy
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.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
Hide Tags
Array Hash
Table
Hide Similar Problems
(M) 3Sum (M)
4Sum (M) Two Sum II - Input array is sorted (E)
Two Sum III - Data structure design
code:
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { map<int,int> m; vector<int> ret; for(int i=0;i<nums.size();i++){ m[nums[i]]=i; } for(int i=0;i<nums.size();i++){ int tmp = target-nums[i]; if(m.find(tmp)!=m.end() && m[tmp]>i){ ret.push_back(i); ret.push_back(m[tmp]); break; } } return ret; } };
相关文章推荐
- 使用Android Studio查看android系统源码
- file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it 的解决
- 《iOS Human Interface Guidelines》——Image View
- 20145314郑凯杰 《Java程序设计》第2周学习总结 代码开始!
- 对于League of Legends的分析
- 四则运算C语言程序
- 杂记_2015.05.22
- 次方编程
- 小白笔记----JAVA小程序数三减一(来自马士兵视屏)
- 软件工程作业—编写四则运算的程序
- eclipse web module版本问题:Cannot change version of project facet Dynamic Web Module to 2.5.
- HTML5+CSS3+JS(第三课).Article元素
- 主成分分析-PCA
- C++ 通过友元函数对类进行iostream流重载
- 实验0:了解和熟悉操作系统
- 九、Null在Java中的精确表示
- Android 单元测试之文件配置
- NotificationListenerService使用小结
- C++/C中的预处理命令;
- Spring4 mvc 基础教程~非常适合新手