class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
int n = (int)nums.size();
for (int i = 0; i < n; i++) {
auto p = map.find(targetnums[i]);
if (p!=map.end()) {
return {p>second+1, i+1};
}
map[nums[i]]=i;
}
}
};
Accepted C++ in 11 lines

@fnatic the member function unordered_map::find() costs O(1),and the time complexity will be O(n) :)


@mako said in Accepted C++ in 11 lines:
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> map; int n = (int)nums.size(); for (int i = 0; i < n; i++) { auto p = map.find(targetnums[i]); if (p!=map.end()) { return {p>second+1, i+1}; } map[nums[i]]=i; } } };
why return p>second +1 and I+1?why +1 I mean?
