#include <unordered_map>
class Solution {
public:
using value_type = int;
using index_type = int;
vector<int> twoSum(vector<int>&nums, int target) {
std::unordered_map<index_type, value_type>mm;
load_map(nums, mm);
int size = nums.size();
int num0 = 0;
int num1 = 0;
for (int i = 0;i < size;i++) {
num0 = nums[i];
num1 = target - num0;
auto it = mm.find(num1);
if (end(mm) == it) {
continue;
}
if (i == it->second) {
continue;
}
return vector<int>{ i, it->second };
}
return vector<int>{};
}
private:
void load_map(const std::vector<int>&array, std::unordered_map<value_type, index_type>&mm) {
int size = array.size();
int num = 0;
for (int i = 0;i < size;i++) {
num = array[i];
mm[num] = i;
}
}
};
leetcode--给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
最新推荐文章于 2024-08-05 16:18:51 发布