1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <vector> #include <iostream>
class Solution { public: void print_unique_pair(const std::vector<int>& arr, int k) { if (arr.empty()) { return; } std::vector<std::pair<int, int>> res; int left = 0, right = arr.size()-1; for (; left < right;) { int val = arr[left] + arr[right]; if (val > k) { right--; } else if (val < k) { left++; } else { if (left == 0 || arr[left] != arr[left-1]) { res.emplace_back(arr[left], arr[right]); } left++, right--; } } for (const auto& x : res) { std::cout << x.first << " " << x.second << std::endl; } }
void print_unique_triple(const std::vector<int>& arr, int k) { if (arr.empty()) { return; } std::vector<std::vector<int>> res; for (int i = 0; i < arr.size(); ++i) { if (i != 0 && arr[i] == arr[i-1]) { continue; } int second = i+1, third = arr.size()-1; for (; second < third;) { int val = arr[i] + arr[second] + arr[third]; if (val > k) { third--; } else if (val < k) { second++; } else { if (arr[second] != arr[second-1]) { res.push_back({arr[i], arr[second], arr[third]}); } second++, third--; } } } for (const auto& x : res) { std::cout << x[0] << " " << x[1] << " " << x[2] << std::endl; } } };
int main() { Solution s; std::vector<int> arr{-8, -4, -3, 0, 1, 1, 2, 4, 5, 8, 9}; s.print_unique_triple(arr, 10); return 0; }
|