Two Sum

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Two Sum相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode #001# Two Sum详解(js描述)

Leetcode - 371. Sum of Two Integers

1 代码片段1

每日一算法之two sum

LeetCode(371) Sum of Two Integers

1_Two Sum --LeetCode