leetcode-1-数组题-两数之和 发表于 2020-03-01 | 分类于 数据结构与算法 题目 解法123456789101112131415161718class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { if (nums.size() <= 0) return {-1, -1}; unordered_map<int, unsigned int> value_pos; for(unsigned int i = 0; i < nums.size(); ++i){ // O(n) if (value_pos.count(target - nums[i]) > 0){ return {value_pos[target-nums[i]], i}; } value_pos[nums[i]] = i; } return {-1, -1}; }};