Untitled
unknown
plain_text
a month ago
978 B
2
Indexable
Never
#define ll long long class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(), nums.end()); int n = nums.size(); vector< vector<int> > output; unordered_set<ll> triplets; for(int i=0;i<n;i++) { int l = i + 1, r = n - 1; while (l < r) { int sum = nums[i] + nums[l] + nums[r]; if (sum == 0) { ll hash = (ll)nums[i] * 1e10L + (ll)nums[l] * 1e5L + (ll)nums[r]; if (triplets.find(hash) == triplets.end()) { vector<int> aux{ nums[i], nums[l], nums[r] }; output.push_back(aux); triplets.insert(hash); } l++, r--; } else if (sum < 0) l++; else r--; } } return output; } };
Leave a Comment