Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> threeSum(vector<int>& nums) {
- sort(nums.begin(), nums.end());
- unordered_map<int,int> M;
- vector<vector<int> > res;
- for(int i = 0; i < nums.size(); i++) {
- M[nums[i]] = i;
- }
- for(int i = 0; i < nums.size(); i++) {
- if(i != 0 && nums[i] == nums[i - 1]) continue;
- int need = -nums[i];
- for(int j = i + 1; j < nums.size(); j++) {
- if(j != i + 1 && nums[j] == nums[j - 1]) continue;
- int req = need - nums[j];
- if(req > nums[nums.size() - 1]) continue;
- if(M.count(req) && M[req] > j) {
- res.push_back({nums[i], nums[j], req});
- }
- }
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement