Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created June 7, 2026 06:47
Show Gist options
  • Select an option

  • Save thinkphp/77fe6e3b794134a46d3d04da474b45e7 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/77fe6e3b794134a46d3d04da474b45e7 to your computer and use it in GitHub Desktop.
solutia optima pentru 2Sum
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
map<int,int> mp;
int n = num.size();
for(int i = 0; i < n; ++i) {
int a = nums[i];
int b = target - a; //calculezi complementarul.
if( mp.find(b) != mp.end() ) {
return { mp[ b ], i };
}
mp[ a ] = i;
}
return {-1,-1};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment