码迷,mamicode.com
首页 > 其他好文 > 详细

Two Sum

时间:2017-10-03 19:43:35      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:span   one   break   specific   numbers   find   int   pre   ber   

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

标签:span   one   break   specific   numbers   find   int   pre   ber   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!