记录自己刷题的日子。。。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
// 1、暴力枚举
int n = nums.size();
for (int i = 0; i < n; i ++)
{
for (int j = i+1; j < n; j++)
{
if (nums[j] == target - nums[i]) return {i, j};
}
}
return {};
// 2、hash
int n = nums.size();
unordered_map<int, int> hashtable;
for (int i = 0; i < n; i ++)
{
auto it = hashtable.find(target - nums[i]);
if (it != hashtable.end())
{
return {i, it->second};
}
hashtable[nums[i]] = i;
}
return {};
}
};