two sum

Posted HUSTLX

tags:

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

#include <string>
#include <stack>
#include <vector>
#include <map>
using namespace std;
vector<int> twoSum(vector<int>& nums, int target) {
    int len = nums.size();
    map<int,int> temp;
    vector<int> re;
    for (int i = 0; i < len; i++) {
        auto it = temp.find(target - nums[i]);
        if (it!= temp.end())
        {
            re.push_back(it->second);
            re.push_back(i);
            return re;
        }
        else temp[nums[i]] = i;
    }
}
int main() {
    vector<int> nums = { 2, 7, 11, 15 };
    int target = 9;
    vector<int> re = twoSum(nums, target);
}

以上是关于two sum的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode Two Sum

1. Two Sum

1. Two Sum

1. Two Sum

Two Sum

Two Sum