leetcode_1-两数之和

时间:2021-04-13 12:55:20   收藏:0   阅读:0

题目

技术图片

代码

#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;
    }
};
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!