# 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(target-nums[i]);
if (p!=map.end()) {
return {p->second+1, i+1};
}
map[nums[i]]=i;
}
}
};``````

• Hi mako,
Your code is amazing now I figure it out! Thanks!

• this ignores the case when nums[i]*2 = target, no?

• if nums={1,1} and target=2, should be no problem. remember it asks for two numbers.

• actually thats not true. not to say by stating"find two numbers from an array index" it usually means finding two different numbers, the problem has explicitly asked

"where index1 must be less than index2"

• This post is deleted!

• it is not O(N).

• This post is deleted!

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

• @fnatic yes . it is O(n^2) .becauce map.find is 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(target-nums[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?

• @sjtuzihan The problem revised since my post. +1 is not needed.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.