Leetcode: Two Sum

原创 2016年08月31日 16:20:34
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int>answer;
       map<int,int>Q;
       for(int i=0;i<nums.size();i++){
            Q.insert(pair<int, int>(nums[i], i));
       }
       for(int i=0;i<nums.size();i++){
//            cout<<nums[i]<<endl;
           if(Q.count(target-nums[i]))
           {
//               cout<<target<<endl;
//               cout<<"yes "<<Q[target-nums[i]]<<endl;
               if(i!=Q[target-nums[i]]){
                   answer.push_back(min(Q[target-nums[i]],i));
                   answer.push_back(max(Q[target-nums[i]],i));
                   return answer;
               }
           }
       }
        return answer;
    }
};

版权声明:本文为博主原创文章,欢迎指教~

相关文章推荐

Two Sum leetcode c++

  • 2015年10月01日 10:47
  • 763B
  • 下载

leetcode 刷题题解(c++) 1.Two Sum (hash表,排序+二分查找)

c++刷leetcode,有题目和算法代码,可直接在leetcode提交通过。

LeetCode - Two Sum

Question: Given an array of integers, return indices of the two numbers such that they add up to a...

LeetCode Two sum

LeetCode Two Sum

[LeetCode]1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific ta...

【LeetCode】.1.Two Sum

1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to...

[leetcode] Two Sum

Hashmap public class Solution { public int[] twoSum(int[] numbers, int target) { int[] result...
  • Otori
  • Otori
  • 2014年11月06日 15:36
  • 416

LeetCode---Two Sum

无意间在CSDN博客上逛时 https://oj.leetcode.com/problems/two-sum/

leetcode_1_Two Sum

最近开始刷leetcode, 这是第一题,没什么复杂的直接上代码好了 题目是:Given an array of integers, return indices of the two number...

Two Sum--LeetCode

题目: Given an array of integers, find two numbers such that they add up to a specific target number....
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode: Two Sum
举报原因:
原因补充:

(最多只允许输入30个字)