leetcode_1-两数之和

题目

技术图片

代码

#include <unordered_map>
#include <vector>

using namespace std;

class Solution {
public:
    vector<int> twoSum(vector<int> &nums, int target) {
        vector<int> res_vec;
        unordered_map<int, int> value_index_map;
        for (int i = 0; i < nums.size(); ++i) {
            auto iter = value_index_map.find(target - nums[i]);
            if (iter != value_index_map.end()) {
                res_vec.emplace_back(iter->second);
                res_vec.emplace_back(i);
                return res_vec;
            }
            value_index_map.emplace(nums[i], i);
        }
        return res_vec;
    }
};

leetcode_1-两数之和

原文地址:https://www.cnblogs.com/zhaoshengwei/p/14652259.html