首页 > 其他 > 详细

Two Sum

时间:2017-10-03 19:38:22      阅读:279      评论:0      收藏:0      [点我收藏+]

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.

 1 class Solution {
 2 public:
 3     vector<int> twoSum(vector<int>& nums, int target) {
 4        unordered_map<int,int>  Mymap;
 5         vector<int> result;
 6        for(int i=0;i<nums.size();i++){
 7            Mymap[nums[i]]=i;
 8        }
 9        for(int i=0;i<nums.size();i++)
10        {
11            auto iter=Mymap.find(target-nums[i]);
12            if(iter!=Mymap.end()&&i!=iter->second){
13                 result.push_back(i);
14                 result.push_back(iter->second);
15                break;
16            } 
17        }
18         return result;
19     }
20 };

 

Two Sum

原文:http://www.cnblogs.com/wsw-seu/p/7624291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!