0
点赞
收藏
分享

微信扫一扫

Leetcode热题100 --1.两数之和

产品喵dandan米娜 2022-03-25 阅读 21
算法哈希

Leetcode热题100 --1.两数之和

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

相关推荐

0 条评论