leetcode-Two Sum
来源:互联网 发布:虚拟商品自动发货源码 编辑:程序博客网 时间:2024/05/16 01:01
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>& nums, int target) { if(nums.empty()) return vector<int>(); map<int,int> index; int size=nums.size(); for(int i=0;i<size;++i) index[target-nums[i]]=i; for(int i=0;i<size;++i){ if(index.find(nums[i])!=index.end()&&index[nums[i]]!=i) return {i+1,index[nums[i]]+1}; } return vector<int>(); }};
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
- iOS完整学习路线图
- 组态王与单片机协议2
- 随意细解:OC -- 类的扩展
- 关于app启动动画如何做到第一次启动采用图片轮播,以后启动都只有1张图片的解决方案
- Hibernate中两种获取Session的方式
- leetcode-Two Sum
- iOS 相机打开动画效果
- 组态王与单片机通信协议3
- [python] 时间序列分析之ARIMA
- swift:制作一个简单的tableheaderview+_navigationbar渐变效果
- 脚本结束 调用函数处理其他事情 register_shutdown_function()
- Opencv3.0.0在Ubuntu12.04下的编译和安装和测试
- 解析XML文档
- HDU 2609 How many (最小表示法)