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

Two Sum

时间:2016-03-15 20:34:17      阅读:169      评论: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.

class Solution {
public:
    vector<int> twoSum(vector<int> &numbers, int target) {
        vector<int>res;
        for(int i=0;i<numbers.size();i++){
            int n=target-numbers[i];
            for(int j=0;j<numbers.size();j++)
                if(j!=i&&numbers[j]==n){
                    if(i>j){
                        int temp=j;
                          j=i;
                        i=temp;
                    }
                res.push_back(i);
                res.push_back(j);
                return res;
            }
    }
    }
};

 

 

Two Sum

标签:

原文地址:http://www.cnblogs.com/wqkant/p/5280821.html

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