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;
    }
};

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

相关文章推荐

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

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

[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编程学习(C语言)】1.Two Sum

LeetCode编程之简单篇,两个数求和,采用穷举和hash对比

[leetcode] Two Sum

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

LeetCode---Two Sum

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

LeetCode 1 - Two Sum

问题简述 Two Sum Given an array of integers, return indices of the two numbers such that th...

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....
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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