SHARE
TWEET

Untitled

a guest Dec 9th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. vector<vector<int>> threeSum(vector<int> &nums) {
  2.   vector<vector<int>> sums;
  3.   unordered_map<int, vector<int>> allnums;
  4.   for (int i = 0; i < nums.size(); i++) {
  5.     auto t = allnums.find(nums[i]);
  6.     if (t == allnums.end()) {
  7.       allnums.emplace(make_pair(nums[i], vector<int>{i}));
  8.     } else {
  9.       t->second.emplace_back(i);
  10.     }
  11.   }
  12.  
  13.   for (int i = 0; i < nums.size(); i++) {
  14.     for (int j = 0; j < nums.size(); j++) {
  15.       if (i == j) {
  16.         continue;
  17.       }
  18.       auto sum = nums[i] + nums[j];
  19.       auto t = allnums.find(0 - sum);
  20.       if (t == allnums.end()) {
  21.         continue;
  22.       }
  23.       int flag = 0;
  24.       for (auto &ind : t->second) {
  25.         if (ind != i && ind != j) {
  26.           flag = 1;
  27.         }
  28.       }
  29.       if (flag) {
  30.         auto a = vector<int>{nums[i], nums[j], t->first};
  31.         sort(a.begin(), a.end());
  32.         if (find(sums.begin(), sums.end(), a) == sums.end()) {
  33.           sums.emplace_back(a);
  34.         }
  35.       }
  36.     }
  37.   }
  38.   return sums;
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top