id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> v;\n vector<vector<int>> v1;\n vector<int> va, vb;\n set<int> sa, sb;\n sort(nums.begin(), nums.end());\n for(int i = 0; i < nums.size(); i++){\n if(nums[i] < 0) va.push_back(nums[i]);\n else vb.push_back(nums[i]);\n }\n sa.insert(va.begin(), va.end());\n sb.insert(vb.begin(), vb.end());\n if(vb.size() == 0) return v1;\n \n if(va.size() == 0){\n if(vb[2] != 0) return v1;\n else{\n v.insert({0, 0, 0});\n for(auto i : v) v1.push_back(i);\n return v1;\n }\n }\n else if(vb.size() > 2){\n if(vb[2] == 0){\n v.insert({0,0,0});\n //for(auto i : v) v1.push_back(i);\n }\n }\n for(int i = 0; i < va.size()-1; i++){\n for(int j = i+1; j < va.size(); j++){\n int a = va[i] + va[j];\n a *= -1;\n auto it = sb.find(a);\n if(it != sb.end()){\n vector<int> vv;\n vv.push_back(va[i]);\n vv.push_back(va[j]);\n vv.push_back(a);\n v.insert(vv);\n }\n }\n }\n\n for(int i = 0; i < vb.size()-1; i++){\n for(int j = i+1; j < vb.size(); j++){\n int a = vb[i] + vb[j];\n a *= -1;\n auto it = sa.find(a);\n if(it != sa.end()){\n vector<int> vv;\n vv.push_back(vb[i]);\n vv.push_back(vb[j]);\n vv.push_back(a);\n v.insert(vv);\n }\n }\n }\n for(auto i : v){\n v1.push_back(i);\n }\n return v1;\n }\n};",
"memory": "194238"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> resSet;\n unordered_map<int,unordered_set<int>> third;\n int zcount = 0;\n for(int i= 0; i< nums.size(); i++){\n if(nums[i] == 0) zcount++;\n third[-nums[i]].insert(i);\n }\n if(zcount > 2) resSet.insert({0,0,0});\n\n for(int i= 0; i< nums.size(); i++){\n for(int j= i+1; j< nums.size(); j++){\n if(third.count(nums[i] + nums[j])){\n unordered_set<int>& intrimSet = third[(nums[i] + nums[j])];\n if(!intrimSet.count(i) && !intrimSet.count(j)){\n vector<int> temp = {nums[i], nums[j], -1 * (nums[i] + nums[j])};\n sort(temp.begin(),temp.end());\n resSet.insert(temp);\n }\n }\n }\n }\n vector<vector<int>> res(resSet.begin(), resSet.end());\n return res;\n }\n};",
"memory": "194238"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "#include <cassert>\n#include <compare>\n#include <ranges>\n#include <span>\n#include <unordered_set>\n#include <utility>\n#include <vector>\n\nstruct addends {\n int n0, n1, n2;\n bool operator==(addends const&) const noexcept = default;\n std::strong_ordering operator<=>(addends const&) const noexcept = default;\n};\n\nstd::size_t hash_combine(std::size_t lhs, std::size_t rhs) {\n return lhs ^ rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2);\n}\n\nnamespace std {\ntemplate <>\nstruct hash<addends>\n{\n std::size_t operator()(addends const& a) const {\n return hash_combine(\n hash_combine(std::hash<int>{}(a.n0), std::hash<int>{}(a.n1)), std::hash<int>{}(a.n2));\n }\n};\n} // namespace std\n\nclass Solution {\npublic:\n\n std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {\n std::ranges::sort(nums);\n std::unordered_set<addends> set;\n int sz = size(nums);\n for (auto i = 0; i < sz - 2; ++i) {\n int n0 = nums[i];\n auto vec = twoSum(span(&nums[i + 1], sz - i - 1), -n0);\n for (auto [n1, n2]: vec)\n set.insert(addends(n0, n1, n2));\n }\n std::vector<std::vector<int>> result;\n result.reserve(size(set));\n for (auto [n0, n1, n2]: set) {\n result.push_back({n0, n1, n2});\n }\n return result;\n }\n\nprivate:\n \n std::vector<std::pair<int,int>> twoSum(std::span<int> nums, int target) {\n std::vector<std::pair<int,int>> vec;\n assert(size(nums) >= 2);\n int first = 0;\n int last = size(nums) - 1;\n do {\n auto sum = nums[first] + nums[last];\n if (sum < target) ++first;\n else if (sum > target) --last;\n else {\n vec.emplace_back(nums[first], nums[last]);\n ++first;\n --last;\n }\n } while (first < last);\n return vec;\n }\n\n};",
"memory": "199149"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& a) {\n int n = a.size();\n sort(a.begin(), a.end());\n auto two_pointers = [&](int l, int r, int target) {\n vector<tuple<int,int>> v;\n while (r > l) {\n if (a[l] + a[r] == target) {\n v.push_back(make_tuple(a[l], a[r]));\n l++, r--;\n } \n if (a[l] + a[r] < target) {\n l++;\n }\n if (a[l] + a[r] > target) {\n r--;\n }\n }\n return v;\n };\n\n set<tuple<int,int,int>> s;\n for (int i = 0; i < n; i++) {\n auto v = two_pointers(i+1, n-1, 0-a[i]);\n for (auto [x, y] : v) {\n s.insert({a[i], x, y});\n }\n }\n vector<vector<int>> ans;\n for (auto [x, y, z] : s) {\n ans.push_back({x, y, z});\n }\n return ans;\n }\n};",
"memory": "199149"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n // int cols=3;\n\n // vector<int> column(cols, 0);\n // Create an empty 2D vector\n std::vector<std::vector<int>> matrix;\n std::sort(nums.begin(), nums.end());\n // std::vector<int> hh;\n int n = nums.size();\n // if(nums[n-1]==0&&nums[0]==0){\n // hh.push_back(0);\n // hh.push_back(0);\n // hh.push_back(0);\n // matrix.push_back(hh);\n // return matrix;\n\n // }\n std::vector<int> nums1;\n\n // Create an unordered_map to keep track of the count of each element\n std::unordered_map<int, int> element_count;\n\n // Iterate through the elements to add\n for (int elem : nums) {\n // Increment the count of the current element\n element_count[elem]++;\n\n // Add the element to the vector if it has appeared less than or\n // equal to 3 times\n if (element_count[elem] <= 3) {\n nums1.push_back(elem);\n }\n }\n // set<int> ss(nums.begin(), nums.end());\n // vector<int> myVectors;\n // vector<int> nums1;\n\n // Convert set to vector\n // for (const auto& row : ss) {\n // myVectors.push_back(row);\n // }\n // for(int i=0;i<myVectors.size();i++){\n // nums1.push_back(myVectors[i]);\n // }\n n=nums1.size();\n for (int i = 0; i < n; i++) {\n int target = nums1[i];\n vector<int> result = twoSum(nums1, (-target), i);\n\n cout << result.size() << endl;\n // cout<<\"target \"<<target<<endl;\n\n if (result.size() == 0) {\n // skip\n } else {\n // cout<<result[0]<<endl;\n // cout<<result[1]<<endl;\n if (result.size() > 2) {\n for (int i = 0; i < result.size(); i += 2) {\n vector<int> m;\n m.push_back(result[i]);\n m.push_back(result[i + 1]);\n m.push_back(target);\n matrix.push_back(m);\n }\n } else {\n result.push_back(target);\n matrix.push_back(result);\n }\n\n // for(int i=0;i<matrix.size();i++){\n // cout<<\"elem \"<<matrix[0][i];\n // }\n }\n }\n for (int i = 0; i < matrix.size(); i++) {\n std::sort(matrix[i].begin(), matrix[i].end());\n }\n set<vector<int>> s(matrix.begin(), matrix.end());\n vector<vector<int>> myVector;\n\n // Convert set to vector\n for (const auto& row : s) {\n myVector.push_back(row);\n }\n\n return myVector;\n }\n\n vector<int> twoSum(vector<int>& numbers, int target, int h) {\n // cout<<\"targetret\"<<target<<endl;\n vector<int> result;\n int n = numbers.size();\n\n for (int i = 0; i < n; ++i) {\n if (i == h)\n continue;\n int x = target - numbers[i];\n if (1) {\n int index = binarySearch(numbers, 0, n - 1, x, i, h);\n if (index != -1) {\n result.push_back(numbers[i]); // 0-based index\n result.push_back(numbers[index]); // 0-based index\n // cout<<\"size2\"<<result.size()<<endl;\n }\n }\n }\n\n return result;\n\n // cout<<\"size1\"<<result.size()<<endl;\n return result; // Return an empty vector if no solution is found\n }\n\nprivate:\n int binarySearch(const vector<int>& arr, int low, int high, int x, int f1,\n int f2) {\n while (low <= high) {\n int mid = low + (high - low) / 2;\n\n // Check if x is present at mid\n if (arr[mid] == x) {\n\n if (mid == f1 || mid == f2) {\n return -1;\n } else {\n return mid;\n }\n }\n\n // If x greater, ignore left half\n if (arr[mid] < x)\n low = mid + 1;\n\n // If x is smaller, ignore right half\n else\n high = mid - 1;\n }\n\n // If we reach here, then element was not present\n return -1;\n }\n};\n\n// class Solution {\n// public:\n// vector<vector<int>> threeSum(vector<int>& nums) {\n// sort(nums.begin() , nums.end()) ;\n// vector<vector<int>>temp;\n// for(int i = 0 ; i < nums.size() ; i++) {\n// if(i>0 && nums[i] == nums[i-1]) continue;\n// int j = i+1;\n// int k = nums.size()-1;\n\n// while(j<k) {\n// int sum = nums[i]+nums[j]+nums[k];\n// if(sum == 0) {\n// vector<int> p = {nums[i],nums[j],nums[k]} ;\n// temp.push_back(p);\n// j++;\n// k--;\n\n// while(j<k && nums[j] == nums[j-1]) j++;\n// while(j<k && nums[k] == nums[k+1]) k--;\n// }\n\n// else if(sum < 0) {\n// j++;\n// }\n\n// else {\n// k--;\n// }\n// }\n\n// }\n\n// return temp;\n// }\n// };",
"memory": "204060"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> trip;\n sort(begin(nums), end(nums));\n if (nums[0] == nums[nums.size() - 1]) {\n if (nums[0] == 0) {\n return {{0,0,0}};\n }\n return {};\n }\n\n for (int i = 0; i < nums.size(); ++i) {\n int l = 0;\n int r = nums.size() - 1;\n int target = -nums[i];\n while (l < r) {\n if (l == i) { ++l; continue; }\n if (r == i) { --r; continue; }\n if (nums[l] + nums[r] == target) {\n vector<int> t { nums[i], nums[l], nums[r] };\n sort(begin(t), end(t));\n trip.insert(std::move(t));\n ++l; --r;\n continue;\n }\n if (nums[l] + nums[r] < target) {\n ++l;\n } else {\n --r;\n }\n }\n }\n\n vector<vector<int>> ans;\n for (auto& v : trip) {\n ans.push_back(std::move(v));\n }\n\n return ans;\n }\n};",
"memory": "204060"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n if (all_of(nums.begin(), nums.end(), [](int i) { return i == 0; }))\n return {{0, 0, 0}};\n sort(nums.begin(), nums.end());\n vector<vector<int>> threeSums;\n int arraySize = nums.size();\n for (int i = 0; i < arraySize; i++) {\n int left = i + 1, right = arraySize - 1;\n while (left < right) {\n int combine = nums[i] + nums[left] + nums[right];\n if (combine == 0) {\n threeSums.push_back({nums[i], nums[left], nums[right]});\n }\n \n if (combine < 0) {\n left++;\n } else {\n right--;\n }\n }\n }\n\n sort(threeSums.begin(), threeSums.end());\n threeSums.erase(unique(threeSums.begin(), threeSums.end()), threeSums.end());\n return threeSums;\n }\n};",
"memory": "208971"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n if (all_of(nums.begin(), nums.end(), [](int i) { return i == 0; }))\n return {{0, 0, 0}};\n sort(nums.begin(), nums.end());\n vector<vector<int>> threeSums;\n int arraySize = nums.size();\n for (int i = 0; i < arraySize; i++) {\n int left = i + 1, right = arraySize - 1;\n while (left < right) {\n int combine = nums[i] + nums[left] + nums[right];\n if (combine == 0) {\n threeSums.push_back({nums[i], nums[left], nums[right]});\n }\n \n if (combine < 0) {\n left++;\n } else {\n right--;\n }\n }\n }\n\n sort(threeSums.begin(), threeSums.end());\n threeSums.erase(unique(threeSums.begin(), threeSums.end()), threeSums.end());\n return threeSums;\n }\n};",
"memory": "208971"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n vector<vector<int>> ans;\n if(nums[0]==0 && nums[nums.size()-1]==0) {\n ans.push_back({0, 0, 0});\n return ans;\n }\n for(int i=0; i<nums.size(); i++) {\n int x = i+1, y=n-1;\n while(x<y && x<n && y>=0) {\n if(nums[i]+nums[x]+nums[y]==0) {\n ans.push_back({nums[i], nums[x], nums[y]}); \n x++; \n } else if(nums[x]+nums[y] > -nums[i]) {\n y--;\n } else {\n x++;\n }\n }\n if(nums[i]>0) break;\n }\n sort(ans.begin(), ans.end());\n vector<vector<int>> ans1;\n if(ans.size()==0) return ans1; \n ans1.push_back(ans[0]);\n int ptr=0;\n for(int i=1;i<ans.size();i++) {\n if(ans[i]!=ans1[ptr]) {\n ans1.push_back(ans[i]);\n ptr++;\n }\n }\n return ans1;\n }\n};",
"memory": "213883"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> answer;\n std::map<int, int> locations;\n std::map<vector<int>, int> test;\n //std::sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size(); i++) {\n locations[nums[i]] = i;\n }\n if (locations.size() == 1 && nums[0] == 0) {\n vector<int> triplet = {0, 0, 0}; \n answer.emplace_back(triplet); \n return answer;\n }\n for (int i = 0; i < nums.size() - 2; i++) {\n for (int j = i + 1; j < nums.size() - 1; j++) {\n const int pre_sum = -(nums[i] + nums[j]);\n const auto it = locations.find(pre_sum);\n if (it != locations.end()) {\n const auto index = it->second;\n if (index == i || index == j) {\n continue;\n }\n vector<int> triplet = {nums[i], nums[j], pre_sum};\n std::sort(triplet.begin(), triplet.end());\n \n if (test.count(triplet) == 0) {\n answer.emplace_back(triplet); \n test[triplet] = 0;\n }\n } \n }\n }\n \n return answer;\n }\n};",
"memory": "213883"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(),nums.end());\n set<vector<int>> ans;\n int j,k;\n if(nums[0] == 0 && nums[n-1] == 0)\n {\n return {{0,0,0}};\n }\n for(int i=0;i<n;i++)\n {\n j=i+1;\n k=n-1;\n while(j<k)\n {\n if(nums[i] + nums[j] + nums[k] == 0)\n {\n vector<int> temp;\n temp.push_back(nums[i]);\n temp.push_back(nums[j]);\n temp.push_back(nums[k]);\n sort(temp.begin(),temp.end());\n ans.insert(temp);\n j++;\n k--;\n }\n else if(nums[i] + nums[j] + nums[k] < 0)\n {\n j++;\n }\n else\n {\n k--;\n }\n }\n \n }\n vector<vector<int>> ans1(ans.begin(),ans.end());\n return ans1;\n }\n};",
"memory": "218794"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n \n set<vector<int>> st;\n int n = nums.size();\n vector<int> temp;\n vector<vector<int>> ans;\n sort(nums.begin() , nums.end());\n if(nums[0] == nums[n-1] && nums[0] == 0){\n temp.push_back(0);\n temp.push_back(0);\n temp.push_back(0);\n ans.push_back(temp);\n return ans;\n }\n\n \n \n for(int i =0; i< n ; i++){\n int s = i+1 ;\n int e = n-1;\n while(s < e ){\n if(nums[i] + nums[s] + nums[e] == 0){\n vector<int> res;\n\n res.push_back(nums[i]);\n res.push_back(nums[s]);\n res.push_back(nums[e]);\n \n if(st.find(res) == st.end())\n ans.push_back(res);\n s++;\n e--;\n st.insert(res);\n }\n else if(nums[i] + nums[s] + nums[e] > 0){\n e--;\n }\n else{\n s++;\n }\n\n }\n }\n\n return ans;\n }\n};",
"memory": "218794"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cout.tie(NULL);\n cin.tie(NULL);\n\n int n = nums.size();\n set<vector<int>> set_store;\n unordered_map<int, int> count;\n for(auto i:nums){\n count[i]++;\n }\n if(count[0]>=3){\n vector<int> t = {0,0,0};\n set_store.insert(t);\n }\n for(int i = 0; i<n; i++){\n for(int j = i+1; j<n; j++){\n while(j<n && nums[j]==0){\n j++;\n }\n if(j==n){\n continue;\n }\n int comp = 0-(nums[i]+nums[j]);\n if(count[comp]>0){\n if((comp==nums[i] || comp==nums[j]) && count[comp]==1){\n continue;\n }else {\n\n vector<int> temp = {nums[i],nums[j],comp};\n if(temp[0] == 0 && temp[1] == 0 && temp[2] == 0 && count[0]<3){\n continue;\n }\n sort(temp.begin(),temp.end());\n set_store.insert(temp);\n }\n }\n\n }\n }\n vector<vector<int>> ans(set_store.begin(),set_store.end());\n return ans;\n\n }\n};",
"memory": "223705"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cout.tie(NULL);\n cin.tie(NULL);\n\n int n = nums.size();\n set<vector<int>> set_store;\n unordered_map<int, int> count;\n for(auto i:nums){\n count[i]++;\n }\n if(count[0]>=3){\n vector<int> t = {0,0,0};\n set_store.insert(t);\n }\n for(int i = 0; i<n; i++){\n for(int j = i+1; j<n; j++){\n while(j<n && nums[j]==0){\n j++;\n }\n if(j==n){\n continue;\n }\n int comp = 0-(nums[i]+nums[j]);\n if(count[comp]>0){\n if((comp==nums[i] || comp==nums[j]) && count[comp]==1){\n continue;\n }else {\n\n vector<int> temp = {nums[i],nums[j],comp};\n if(temp[0] == 0 && temp[1] == 0 && temp[2] == 0 && count[0]<3){\n continue;\n }\n sort(temp.begin(),temp.end());\n set_store.insert(temp);\n }\n }\n\n }\n }\n vector<vector<int>> ans(set_store.begin(),set_store.end());\n return ans;\n\n }\n};",
"memory": "228616"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // int find_index(vector<int> v, int index1, int index2)\n // {\n // int elem = -(v[index1] + v[index2]);\n // int start = 0, end = int(v.size()) -1 ;\n // while(start <= end)\n // {\n // int mid = (start + end)/2;\n // if(v[mid] == elem)\n // {\n // if(mid == index1 || mid == index2)\n // {\n // if(mid - 1 >= 0 && v[mid-1] == elem && mid-1 != index1 && mid-1 != index2)\n // {\n // return mid -1;\n // }\n // else if(mid + 1 <= int(v.size()) - 1 && v[mid + 1] == elem && mid+1 != index1 && mid+1 != index2)\n // { \n // return mid + 1;\n // }\n // else\n // {\n // return -1;\n // }\n // }\n // return mid;\n // }\n // else if(elem > v[mid])\n // {\n // start = mid+ 1; \n // }\n // else // elem < v[mid]\n // {\n // end = mid-1;\n // }\n // }\n // return -1;\n // }\n // vector<vector<int>> check_2sum(vector<int> v, int index1)\n // {// returns all pairs with sum -(v[index1])\n // vector<vector<int>> vec_2sum ;\n // for(int index2 = 0; index2 < int(v.size()); index2++)\n // {\n // if(index2 != index1)\n // {\n // int index3 = find_index(v, index1, index2);\n // if(index3 != -1)\n // {\n // // triplet found\n // vector<int> v1 = {v[index1], v[index2], v[index3]};\n // sort(v1.begin(), v1.end());\n // vec_2sum.push_back(v1);\n // }\n // }\n // }\n // return vec_2sum;\n // }\n vector<vector<int>> find_2sum_pairs(vector<int> v, int index)\n {\n int sum = -v[index];\n int p1 = 0, p2 = int(v.size())-1;\n vector<vector<int>> ans;\n while(p1 < p2)\n {\n if(v[p1] + v[p2] == sum )\n {\n if(p1 != index && p2 != index)\n {\n vector<int> temp = {v[index], v[p1], v[p2]};\n sort(temp.begin(), temp.end());\n ans.push_back(temp);\n }\n p1++; p2--;\n }\n else if(v[p1] + v[p2] < sum)\n {\n ++p1; // we need to increase the pair sum\n }\n else // v[p1] + v[p2] > sum --- then we need to decrease the pair sum\n {\n --p2;\n }\n }\n return ans;\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n // cout<<\"\\n\\n\";\n // cout<<int(nums.size());\n // cout<<\"\\n\\n\";\n sort(nums.begin(), nums.end()); \n set<vector<int>> set_triplets;\n vector<int> nums_temp;\n for(auto& elem: nums)\n {\n if(int(nums_temp.size()) == 0 || int(nums_temp.size()) == 1)\n {\n nums_temp.push_back(elem);\n }\n else if(nums_temp[int(nums_temp.size()) - 1] != elem)\n {\n nums_temp.push_back(elem);\n }\n else if(nums_temp[int(nums_temp.size()) - 1] == elem && nums_temp[int(nums_temp.size()) - 2] != elem)\n {\n nums_temp.push_back(elem);\n }\n else if( (int(nums_temp.size()) == 2 && nums_temp[int(nums_temp.size()) - 1] == 0 && nums_temp[int(nums_temp.size()) - 2] == 0 && elem == 0)\n || (int(nums_temp.size()) > 2 && nums_temp[int(nums_temp.size()) - 1] == 0 && nums_temp[int(nums_temp.size()) - 2] == 0 && nums_temp[int(nums_temp.size()) -3 != 0 ] && elem == 0) )\n {\n nums_temp.push_back(elem);\n }\n }\n for(int i = 0 ; i < int(nums_temp.size()); i++)\n {\n vector<vector<int>> temp_vec = find_2sum_pairs(nums_temp, i);\n for(auto& vec: temp_vec)\n {\n set_triplets.insert(vec);\n }\n }\n vector<vector<int>> ans(set_triplets.begin(), set_triplets.end());\n // vector<vector<int>> ans = {{1, 8, 9}};\n return ans;\n // return set_triplets;\n }\n};",
"memory": "233528"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int size = nums.size();\n sort(nums.begin(), nums.end());\n\n if(size<3)\n return {{}};\n \n if(nums[0] == nums[size-1] && nums[0] == 0)\n return {{nums[0], nums[0], nums[0]}};\n\n set<vector<int>> ans;\n for(int first = 0; first < size; first++) {\n int second = 0;\n int third = size-1;\n\n int requiredSum = -1*nums[first];\n while(second<third) {\n if(second==first)\n second++;\n else if(third==first)\n third--;\n else {\n if(requiredSum == nums[second] + nums[third]) {\n vector<int> temp = {nums[first], nums[second], nums[third]};\n sort(temp.begin(), temp.end());\n ans.insert(temp);\n second++;\n }\n else if(requiredSum > nums[second] + nums[third])\n second++;\n else \n third--;\n }\n }\n \n }\n\n vector<vector<int>> answer;\n for(auto a: ans)\n answer.push_back(a);\n \n return answer;\n }\n};",
"memory": "233528"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n \n if(nums.size()<3) return {};\n \n sort(nums.begin(), nums.end());\n int n = nums.size();\n \n if(nums.front() == 0 && nums.back() ==0) return vector<vector<int>>(1, vector<int>(3,0));\n \n if(nums[0] > 0) return {};\n vector<vector<int>>ans;\n \n unordered_map<int, int>mp;\n set<vector<int>>st;\n \n for(int i=0; i<n; i++)\n {\n mp[nums[i]] = i;\n }\n \n for(int i=0; i<n-2; i++)\n {\n int sum=0;\n sum+=nums[i];\n // vector<int>v1;\n \n for(int j=i+1; j<n-1; j++)\n {\n vector<int>v1;\n sum+=nums[j];\n int neg_sum = 0-sum;\n if(mp.find(neg_sum) != mp.end() && mp[neg_sum] > j)\n {\n v1.push_back(nums[i]);\n v1.push_back(nums[j]);\n v1.push_back(neg_sum);\n \n if(st.find(v1) == st.end()) \n {\n ans.push_back(v1);\n st.insert(v1);\n }\n // break;\n }\n \n sum-=nums[j];\n }\n }\n \n \n return ans;\n }\n};",
"memory": "258084"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // vector<int> twosum(int i,vector<int>&nums){\n // int sum = -nums[i];\n // for(;i<s;i++){\n\n // }\n // }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> res;\n set<vector<int>>gabbu;\n int n = nums.size();\n sort(nums.begin(),nums.end());\n if(nums[0] == nums[n-1] && nums[0] == 0){\n vector<int> arr= {0,0,0};\n res.push_back(arr);\n return res;\n }\n for(int i=0;i<n-2;i++){\n int sum = nums[i];\n sum = -1*sum;\n int j = i+1;\n int k = n-1;\n while(k>j){\n if(nums[j] + nums[k]<sum){\n j++;\n continue;\n }\n if(nums[j]+nums[k] >sum){\n k--;\n continue;\n }\n if(nums[j] +nums[k] == sum){\n vector<int > arr;\n arr.push_back(-1*sum);\n arr.push_back(nums[j]);\n arr.push_back(nums[k]);\n gabbu.insert(arr);\n j++;\n continue;\n }\n }\n }\n for(auto k:gabbu){\n res.push_back(k);\n }\n return res;\n }\n};",
"memory": "258084"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\n typedef vector<int> triple;\n\npublic:\n vector<vector<int>> threeSum(vector<int>& nums_in) {\n auto nums = nums_in;\n set<vector<int>> solutionsSet;\n vector<set<int>> sub_solutions;\n auto const nums_count = nums.size();\n vector<vector<int>> solutions;\n sort(nums.begin(), nums.end());\n\n const int min_nums_i = nums[0];\n for (size_t j = 0; j < nums_count - 1; ++j) sub_solutions.push_back(std::set<int>());\n for (size_t j = 1; j < nums_count - 1; ) {\n const int max_nums_i = nums[j-1];\n // cout << \"j=\" << j << \", range: [\" << min_nums_i << \", \" << max_nums_i <<\"]\" << endl;\n std::set<int> sub_sol_j;\n size_t k = j + 1, k0 = k;\n size_t k1 = nums_count - 1;\n const int lower_bound_k = -(nums[j] + max_nums_i);\n do { \n //cout << k0 << \" \" << k << \" \" << k1 << \" -> \";\n if (nums[k] > lower_bound_k) {\n k1 = k;\n } else {\n k0 = k;\n }\n k = (k0+k1) / 2;\n //cout << k0 << \" \" << k << \" \" << k1 << endl;\n } while (k < nums_count && k0 + 1 < k1);\n for (; k < nums_count;) {\n if (nums[j] + nums[k] + min_nums_i >0) break;\n if (!(nums[j] + nums[k] + max_nums_i <0)) {\n // cout << \"j=\" << j << \", k=\" << k << endl;\n sub_sol_j.insert(nums[j] + nums[k]);\n }\n do {\n ++k;\n } while (false && (k < nums_count) && (nums[k - 1] == nums[k]));\n }\n sub_solutions[j] = sub_sol_j;\n /*cout << \"sub_solutions[\" << j << \"] <- \";\n for (auto s:sub_solutions[j]) {\n cout << s << \", \";\n }\n cout << endl;*/\n\n do {\n ++j;\n } while (false && (j < nums_count-1) && (nums[j - 1] == nums[j]));\n }\n\n for (size_t i = 0; i < nums_count - 2;) {\n for (size_t j = i + 1; j < nums_count - 1; ) {\n if (sub_solutions[j].count(-nums[i]) > 0) {\n triple s;\n s.push_back(nums[i]);\n s.push_back(nums[j]);\n s.push_back(-nums[i] - nums[j]);\n solutionsSet.insert(s);\n // solutionsSet.insert(*new triple(s));\n }\n do {\n ++j;\n } while ((j < nums_count - 1) && (nums[j - 1] == nums[j]));\n }\n do {\n ++i;\n } while ((i < nums_count - 2) && (nums[i - 1] == nums[i]));\n }\n for (auto s : solutionsSet) {\n solutions.push_back(s);\n }\n /*\n for (auto s2 : sub_solutions) {\n for (auto sum2:s2.second) {\n triple s;\n s.push_back(s2.first);\n s.push_back(sum2);\n s.push_back(0);\n solutions.push_back(s);\n }\n }\n */\n\n return solutions;\n }\n};",
"memory": "262995"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> retset={};\n std::sort(std::begin(nums),std::end(nums));\n // auto middle=std::begin(nums)+nums.size()/2;\n auto left=std::begin(nums);\n bool loopcondition=true;\n for(;left<std::end(nums);++left){\n if((left>std::begin(nums))&&((*left)==(*(left-1)))){\n ++left;\n }\n auto mid=left+1;\n auto right=std::end(nums)-1;\n while(mid<right){\n int tempsum=(*left)+(*right)+(*mid);\n if(tempsum<0){\n ++mid;\n } else if(tempsum>0){\n --right;\n } else{\n retset.insert(std::vector<int>{(*left),(*mid),(*right)});\n ++mid;\n }\n }\n }\n vector<vector<int>> retval(std::begin(retset),std::end(retset));\n return retval;\n }\n};\n",
"memory": "262995"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int target = 0;\n sort(nums.begin(), nums.end());\n int n = nums.size();\n set<vector<int>> ansSet;\n vector<vector<int>> ansVector;\n for(int i=0;i<n;i++) {\n int sumNeeded = target - nums[i];\n int l=i+1, h=n-1;\n while (h>l && h<n && l>=0) {\n if (nums[l]+nums[h]==sumNeeded) {\n ansSet.insert({nums[i], nums[l], nums[h]});\n if (nums[l+1]==nums[l] && nums[h]!=nums[h-1]) l++;\n else if (nums[l+1]!=nums[l] && nums[h]==nums[h-1]) h--;\n else if (nums[l+1]==nums[l] && nums[h]==nums[h-1]) h--;\n else l++;h--;\n } else if(nums[l]+nums[h] > sumNeeded){\n h--;\n } else if(nums[l]+nums[h] < sumNeeded){\n l++;\n }\n }\n }\n for (vector<int> x: ansSet) {\n ansVector.push_back(x);\n }\n return ansVector;\n \n }\n};",
"memory": "267906"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\ntemplate <typename T> using vt = vector<T>;\ntypedef uint32_t u32;\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vt<vt<int>> ans;\n \n sort(nums.begin(), nums.end());\n \n u32 n = nums.size();\n\n u32 count = 1;\n int current = 0;\n for (u32 i = 0; i < n; i++) {\n if (nums[i] != current) {\n count = 1;\n current = nums[i];\n } else if (count > 3) {\n nums.erase(nums.begin() + i);\n n--;\n } else {\n count++;\n }\n }\n \n for (u32 cp = 0; cp < n; cp++) {\n int c = nums[cp];\n u32 ap = 0, bp = n - 1;\n \n for (; ap < bp && ap < cp && cp < bp;) {\n if (abs((double) ap-bp) <= 1) {\n break;\n }\n\n int absum = nums[ap] + nums[bp];\n\n if (absum < -c) {\n ap++;\n } else if (absum > -c) {\n bp--;\n } else {\n vector<int> subans =\n {nums[ap], nums[bp], nums[cp]};\n sort(subans.begin(), subans.end());\n ans.push_back(subans);\n ap++;\n bp--;\n }\n }\n }\n\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n\n return ans;\n }\n};\n",
"memory": "272818"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n std::vector<std::vector<int>> res;\n std::sort(nums.begin(), nums.end());\n for(size_t i = 0; i < nums.size(); ++i){\n int target = -nums[i];\n int l = i + 1;\n int r = nums.size() - 1;\n while (l < r) {\n int sum = nums[l] + nums[r];\n if(sum == target){\n std::vector<int> triplet = {nums[i], nums[l], nums[r]};\n if(auto it = std::lower_bound(res.begin(), res.end(), triplet); it == res.end() || *it != triplet)\n res.push_back(std::move(triplet));\n l++; r--;\n } else {\n if(sum < target)\n l++;\n else\n r--;\n }\n }\n }\n return res;\n }\n};",
"memory": "277729"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n\n /* question says order is not important but the API says order is important. thats why this line: */\n \n\n std::vector<std::vector<int>> result;\n std::map<std::string,std::vector<int>> r;\n \n /* remove duplicates for optimization */\n std::map<int,int> tmp;\n for(auto e:nums)\n {\n \n tmp[e]++;\n }\n \n\n std::vector<int> nums2;\n \n for(auto e:tmp)\n {\n if(e.second == 1)\n nums2.push_back(e.first);\n else if(e.second == 2)\n {\n nums2.push_back(e.first); \n nums2.push_back(e.first); \n }\n else if(e.second > 2)\n {\n nums2.push_back(e.first); \n nums2.push_back(e.first); \n nums2.push_back(e.first); \n }\n }\n nums = nums2;\n const int n = nums.size();\n\n std::map<int,bool> cache1;\n for(int i=0;i<n;i++)\n cache1[nums[i]]=true;\n std::map<int,std::vector<std::vector<int>>> cache2;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n {\n if(i!=j && cache1.find(-nums[i]-nums[j]) != cache1.end())\n {\n if(cache2.find(-nums[i]-nums[j])==cache2.end())\n {\n cache2[-nums[i]-nums[j]]={{i,j}};\n }\n else\n {\n cache2[-nums[i]-nums[j]].push_back({i,j});\n }\n }\n }\n\n for(int i=0;i<n;i++)\n {\n if(cache2.find(nums[i])!=cache2.end())\n {\n auto index = cache2[nums[i]];\n for(auto e:index)\n {\n if(e[0] == i || e[1] == i) continue;\n \n int tmp[3]={nums[e[0]],nums[e[1]],nums[i]};\n std::sort(tmp,tmp+3);\n r[\n std::to_string(tmp[0])+\"_\"+\n std::to_string(tmp[1])+\"_\"+\n std::to_string(tmp[2])\n ]=\n {\n tmp[0],\n tmp[1],\n tmp[2]\n };\n }\n }\n }\n\n for(auto e:r)\n result.push_back(e.second);\n return result;\n }\n};",
"memory": "282640"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n\n /* question says order is not important but the API says order is important. thats why this line: */\n \n\n std::vector<std::vector<int>> result;\n std::map<std::string,std::vector<int>> r;\n \n /* remove duplicates for optimization */\n std::map<int,int> tmp;\n for(auto e:nums)\n {\n \n tmp[e]++;\n }\n \n\n std::vector<int> nums2;\n \n for(auto e:tmp)\n {\n if(e.second == 1)\n nums2.push_back(e.first);\n else if(e.second == 2)\n {\n nums2.push_back(e.first); \n nums2.push_back(e.first); \n }\n else if(e.second > 2)\n {\n nums2.push_back(e.first); \n nums2.push_back(e.first); \n nums2.push_back(e.first); \n }\n }\n nums = nums2;\n const int n = nums.size();\n\n std::map<int,bool> cache1;\n for(int i=0;i<n;i++)\n cache1[nums[i]]=true;\n std::map<int,std::vector<std::vector<int>>> cache2;\n for(int i=0;i<n;i++)\n for(int j=0;j<n;j++)\n {\n if(i!=j && cache1.find(-nums[i]-nums[j]) != cache1.end())\n {\n if(cache2.find(-nums[i]-nums[j])==cache2.end())\n {\n cache2[-nums[i]-nums[j]]={{i,j}};\n }\n else\n {\n cache2[-nums[i]-nums[j]].push_back({i,j});\n }\n }\n }\n\n for(int i=0;i<n;i++)\n {\n if(cache2.find(nums[i])!=cache2.end())\n {\n auto index = cache2[nums[i]];\n for(auto e:index)\n {\n if(e[0] == i || e[1] == i) continue;\n \n int tmp[3]={nums[e[0]],nums[e[1]],nums[i]};\n std::sort(tmp,tmp+3);\n r[\n std::to_string(tmp[0])+\"_\"+\n std::to_string(tmp[1])+\"_\"+\n std::to_string(tmp[2])\n ]=\n {\n tmp[0],\n tmp[1],\n tmp[2]\n };\n }\n }\n }\n\n for(auto e:r)\n result.push_back(e.second);\n return result;\n }\n};",
"memory": "287551"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int i = 0;\n int j = nums.size() - 1;\n sort(nums.begin(),nums.end());\n vector<vector<int>> res;\n while(i<j-1){\n int tmp = 0 - nums[i];\n int k = i + 1;\n while(k<j){\n if(nums[k]+nums[j]==tmp){\n vector<int> tmp1 = {nums[i],nums[k],nums[j]};\n bool valid = true;\n for(int x = 0; x < res.size(); x++){\n if(res[x][0]==tmp1[0]&&res[x][1]==tmp1[1]&&res[x][2]==tmp1[2]){\n valid = false;\n break;\n }\n }\n if(valid){\n res.push_back(tmp1);\n }\n k++;\n j--;\n }\n else if(nums[k]+nums[j]>tmp){\n j--;\n }\n else{\n k++;\n }\n }\n i++;\n j = nums.size() - 1;\n }\n return res;\n }\n};",
"memory": "287551"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n set<vector<int>> triplets;\n int p1, p2, p3, sum;\n for(int i=0; i<nums.size()-2; i++) {\n p1 = i;\n sum = nums[p1];\n p2 = p1+1;\n p3 = nums.size()-1;\n while(p2 < p3) {\n if(nums[p2] + nums[p3] + sum == 0) {\n triplets.insert({nums[p1], nums[p2], nums[p3]});\n p2++;\n p3--;\n }\n else if(nums[p2] + nums[p3] + sum > 0) {\n p3--;\n }\n else p2++;\n }\n }\n vector<vector<int>> vc(triplets.begin(), triplets.end());\n\n return vc;\n }\n};",
"memory": "292463"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n \n set<vector<int>> uniqueTriplets;\n for(int i = 0;i<nums.size();i++){\n int m = i+1,n=nums.size()-1;\n \n while(m<n){\n int sum = nums[m]+nums[n];\n if(sum+nums[i]==0){\n uniqueTriplets.insert({nums[i], nums[m], nums[n]});\n m++;\n n--;\n }\n else if(sum<-nums[i])m++;\n else n--;\n }\n \n }\n return vector<vector<int>>(uniqueTriplets.begin(), uniqueTriplets.end());\n \n }\n};",
"memory": "292463"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n vector<vector<int>> final;\n set<vector<int>> s;\n for(int i=0 ; i< nums.size(); ++i){\n \n int val = -nums[i];\n cout<<val;\n int j=i+1;\n int k=nums.size()-1;\n while( j < k ){\n \n if(nums[j] + nums[k] == val){\n vector<int> a = {nums[i],nums[j],nums[k]};\n sort(a.begin(),a.end());\n if(s.empty()){\n s.insert(a);\n final.push_back(a);\n }\n else if( s.find(a)==s.end()){\n s.insert(a);\n final.push_back(a);\n }\n --k;\n ++j;\n }\n else if(nums[j] + nums[k] > val){\n --k;\n }\n else if(nums[j] + nums[k] < val){\n ++j;\n }\n }\n \n }\n return final;\n }\n};",
"memory": "297374"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct CompareByFirstElement {\n bool operator() (const std::tuple<int, int>& a, const std::tuple<int, int>& b) const {\n return std::get<0>(a) < std::get<0>(b);\n}\n };\n vector<vector<int>> threeSum(vector<int>& nums) {\n std::vector<std::tuple<int, int>> vec ;\n for(unsigned int i = 0; i < nums.size(); ++i){\n vec.push_back(std::make_tuple(nums[i], i));\n }\n std::sort(vec.begin(), vec.end(), CompareByFirstElement());\n std::set<std::vector<int>> out;\n for(int i = 0; i < nums.size() - 2; ++i){\n int target = -nums[i];\n int left = 0;\n int right = nums.size()-1;\n while(left<right){\n if(std::get<1>(vec[left]) <= i){\n ++left;\n }else if(std::get<1>(vec[right]) <= i){\n --right;\n }else{\n int t = std::get<0>(vec[left]) + std::get<0>(vec[right]);\n if(t < target){\n ++left;\n }else if(t>target){\n --right;\n }else{\n std::vector<int> temp = {nums[i],std::get<0>(vec[left]),std::get<0>(vec[right])};\n std::sort(temp.begin(),temp.end());\n out.insert(temp);\n --right;\n ++left;\n }\n }\n }\n }\n std::vector<std::vector<int>> o (out.begin(),out.end());\n return o;\n }\n};",
"memory": "297374"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n=nums.size();\n vector<vector<int>>ans;\n set<vector<int>>ans1;\n if(nums.empty()||n<3){\n return ans;\n }\n sort(nums.begin(),nums.end());\n for(int i=0;i<n-2;i++){\n int x=nums[i];\n int j=i+1;\n int k=n-1;\n while(j<k){\n int sum=x+nums[j]+nums[k];\n if(sum==0){\n // if(nums[j]!=nums[k])\n ans1.insert({x,nums[j],nums[k]});\n j++;\n k--;\n }\n else if(sum<0){\n j++;\n }\n else {\n k--;\n }\n }\n \n }\n for(auto i:ans1){\n vector<int>res;\n for(auto j:i){\n res.push_back(j);\n }\n ans.push_back(res);\n }\n return ans;\n }\n};",
"memory": "302285"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums)\n {\n vector<vector<int>> out;\n sort(nums.begin(), nums.end());\n\n for (int i = nums.size()-1; i >= 2; i--)\n {\n vector<vector<int>> ret = twoSum(nums, i);\n out.insert(out.end(), ret.begin(), ret.end());\n }\n\n return out;\n }\n\nprivate:\n set<vector<int>> hmap;\n\n vector<vector<int>> twoSum(vector<int>& numbers, int size)\n {\n vector<vector<int>> out = {};\n int left = 0, right = size-1;\n int target = - numbers[size];\n\n while (left < right)\n {\n int sum = numbers[left] + numbers[right];\n\n if (sum == target)\n {\n vector<int> triplet = {numbers[left], numbers[right], numbers[size]};\n if (hmap.find(triplet) == hmap.end())\n {\n out.push_back(triplet);\n hmap.insert(triplet);\n }\n left++;\n right--;\n }\n else if (sum > target)\n right--;\n else\n left++;\n }\n\n return out;\n }\n};",
"memory": "302285"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n void merge(vector<int> &nums,int s, int mid, int e){\n vector<int> n1(mid-s+1);\n vector<int> n2(e-mid);\n for(int i=0;i<=mid-s;i++)\n {\n n1[i]=nums[s+i];\n }\n \n for(int j=0;j<e-mid;j++){\n n2[j]=nums[j+mid+1];\n }\n \n int count=0, i=0, j=0;\n while(i<(mid-s+1)&&j<(e-mid)){\n if(n1[i]>n2[j]){\n nums[count+s]=n2[j];\n j++;\n }\n else{\n nums[count+s]=n1[i];\n i++;\n }\n count++;\n }\n \n while(i<(mid-s+1)){\n nums[count+s]=n1[i];\n i++;\n count++;\n }\n \n while(j<(e-mid)){\n nums[count+s]=n2[j];\n j++;\n count++;\n }\n }\n \n void mergeSort(vector<int> &nums, int s, int e){\n if(s<e)\n {\n int mid=s+(e-s)/2;\n mergeSort(nums, s, mid);\n mergeSort(nums, mid+1, e);\n merge(nums, s, mid, e);\n }\n }\n \n \n vector<vector<int>> threeSum(vector<int>& nums) \n {\n multimap<int, int> ma;\n for(int i=0;i<nums.size();i++){\n ma.insert({nums[i],i});\n }\n set<vector<int>> ans;\n mergeSort(nums,0,nums.size()-1);\n for(int i=0;i<nums.size()-2;i++)\n {\n for(int j=i+1, k=nums.size()-1;j<k;)\n {\n if(nums[j]+nums[k]+nums[i]<0)\n {\n j++;\n }\n else if(nums[j]+nums[k]+nums[i]>0)\n {\n k--;\n }\n else\n {\n vector<int> tm(3);\n tm[0]=nums[i];\n \n tm[1]=nums[j];\n \n tm[2]=nums[k];\n ans.insert(tm);\n j++;\n k--;\n }\n }\n \n }\n vector<vector<int>> ans2;\n \n int level=0;\n \n for (vector<int> v: ans){\n ans2.push_back(v); \n }\n \n return ans2;\n }\n};",
"memory": "307196"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n void merge(vector<int> &nums,int s, int mid, int e){\n vector<int> n1(mid-s+1);\n vector<int> n2(e-mid);\n for(int i=0;i<=mid-s;i++)\n {\n n1[i]=nums[s+i];\n }\n \n for(int j=0;j<e-mid;j++){\n n2[j]=nums[j+mid+1];\n }\n \n int count=0, i=0, j=0;\n while(i<(mid-s+1)&&j<(e-mid)){\n if(n1[i]>n2[j]){\n nums[count+s]=n2[j];\n j++;\n }\n else{\n nums[count+s]=n1[i];\n i++;\n }\n count++;\n }\n \n while(i<(mid-s+1)){\n nums[count+s]=n1[i];\n i++;\n count++;\n }\n \n while(j<(e-mid)){\n nums[count+s]=n2[j];\n j++;\n count++;\n }\n }\n \n void mergeSort(vector<int> &nums, int s, int e){\n if(s<e)\n {\n int mid=s+(e-s)/2;\n mergeSort(nums, s, mid);\n mergeSort(nums, mid+1, e);\n merge(nums, s, mid, e);\n }\n }\n \n \n vector<vector<int>> threeSum(vector<int>& nums) \n {\n multimap<int, int> ma;\n for(int i=0;i<nums.size();i++){\n ma.insert({nums[i],i});\n }\n set<vector<int>> ans;\n mergeSort(nums,0,nums.size()-1);\n for(int i=0;i<nums.size()-2;i++)\n {\n //int j=i+1, k=nums.size()-1;\n \n for(int j=i+1, k=nums.size()-1;j<k;)\n {\n if(nums[j]+nums[k]+nums[i]<0)\n {\n j++;\n }\n else if(nums[j]+nums[k]+nums[i]>0)\n {\n k--;\n }\n else //if(nums[j]+nums[k]+nums[i]==0)\n {\n vector<int> tm(3);\n tm[0]=nums[i];\n \n tm[1]=nums[j];\n \n tm[2]=nums[k];\n ans.insert(tm);\n j++;\n k--;\n }\n }\n \n }\n //cout<<ans.size()<<endl;\n vector<vector<int>> ans2;\n \n int level=0;\n \n for (vector<int> v: ans){\n ans2.push_back(v); \n }\n \n return ans2;\n }\n};",
"memory": "307196"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> result;\n\n sort(nums.begin(), nums.end());\n\n int sum = 0;\n\n for(int i : nums){\n if(i != 0){\n sum = 1;\n break;\n }\n }\n\n if(sum == 0){\n return vector<vector<int>> {{0, 0, 0}};\n }\n\n for(int i = 0; i < nums.size()-2; i++){\n int target = -nums[i];\n int low = i+1;\n int high = nums.size()-1;\n while(high > low){\n if(nums[high] + nums[low] > target){\n high--;\n }\n else if(nums[high] + nums[low] < target){\n low++;\n }\n else{\n vector<int> temp = {nums[i], nums[low], nums[high]};\n sort(temp.begin(), temp.end());\n result.emplace(temp);\n low++;\n high--;\n }\n }\n\n }\n vector<vector<int>> ans;\n\n for(auto i : result){\n ans.emplace_back(i);\n }\n\n return ans;\n\n }\n};",
"memory": "312108"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> result;\n sort(nums.begin(), nums.end());\n \n for (int i = 0; i < nums.size() - 2; i++) {\n auto twoSumList = twoSum(nums, i + 1, -nums[i]);\n for (auto list : twoSumList) {\n result.insert({nums[i], list[0], list[1]});\n }\n }\n vector<vector<int>> expectedResult;\n for (auto entry: result) {\n expectedResult.push_back(entry);\n }\n return expectedResult;\n }\n \nprivate:\n set<vector<int>> twoSum(const vector<int>& nums, int startIndex, int target) {\n set<vector<int>> result = {};\n int endIndex = nums.size() - 1;\n while (startIndex < endIndex) {\n auto sum = nums[startIndex] + nums[endIndex];\n if (sum == target) {\n result.insert({nums[startIndex], nums[endIndex]});\n startIndex++;\n endIndex--;\n } else if (sum < target) {\n startIndex++;\n } else {\n endIndex--;\n }\n }\n return result;\n }\n};",
"memory": "312108"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>>ans;\n vector<int>mp(2000000);\n vector<int>v;\n for(auto i:nums) {\n mp[i + 200000]++;\n if(mp[i + 200000] == 1)\n v.push_back(i);\n }\n n = v.size();\n for(int i = 0;i < n;i++){\n int a = v[i];\n for(int j = i + 1;j < n;++j){\n int b = v[j];\n int c = 0 - (a + b);\n if ( mp[c + 200000] == 0 ) continue;\n else if ( (c == a || c == b) && mp[c + 200000] <= 1)\n continue; \n else if ( c == a && a == b && mp[c + 200000] <= 2) \n continue;\n else ans.push_back({a,b,c});\n }\n }\n if(mp[200000] >= 3) ans.push_back({0,0,0});\n for(auto &i:ans) \n sort(i.begin(),i.end());\n sort(ans.begin(),ans.end());\n ans.resize(unique(ans.begin(),ans.end()) - ans.begin());\n return ans;\n }\n};",
"memory": "317019"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>>ans;\n vector<int>mp(2000000);\n vector<int>v;\n for(auto i:nums) {\n mp[i + 200000]++;\n if(mp[i + 200000] == 1)\n v.push_back(i);\n }\n n = v.size();\n for(int i = 0;i < n;i++){\n int a = v[i];\n for(int j = i + 1;j < n;++j){\n int b = v[j];\n int c = 0 - (a + b);\n if ( mp[c + 200000] == 0 ) continue;\n else if ( (c == a || c == b) && mp[c + 200000] <= 1)\n continue; \n else if ( c == a && a == b && mp[c + 200000] <= 2) \n continue;\n else ans.push_back({a,b,c});\n }\n }\n if(mp[200000] >= 3) ans.push_back({0,0,0});\n for(auto &i:ans) \n sort(i.begin(),i.end());\n sort(ans.begin(),ans.end());\n ans.resize(unique(ans.begin(),ans.end()) - ans.begin());\n return ans;\n }\n};",
"memory": "317019"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> res;\n sort(nums.begin(), nums.end());\n\n int n = nums.size();\n unordered_map<int, bool> map;\n for(int i = n-1; i >= 2; i--)\n {\n if (nums[i] < 0) break;\n if (i < n-1 && nums[i]==nums[i+1]) continue;\n for (int j = 0; j < i; j++)\n {\n int key = -nums[i]-nums[j];\n if (map.contains(key) && !map[key])\n {\n res.push_back({key, nums[j], nums[i]});\n map[key] = true;\n map[nums[j]] = true;\n }\n else if (!map.contains(key))\n {\n map[nums[j]] = false;\n }\n }\n map.clear();\n }\n return res;\n }\n};",
"memory": "321930"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> res;\n sort(nums.begin(), nums.end());\n\n int n = nums.size();\n unordered_map<int, bool> map;\n for(int i = n-1; i >= 2; i--)\n {\n if (nums[i] < 0) break;\n if (i < n-1 && nums[i]==nums[i+1]) continue;\n for (int j = 0; j < i; j++)\n {\n int key = -nums[i]-nums[j];\n if (map.contains(key) && !map[key])\n {\n res.push_back({key, nums[j], nums[i]});\n map[key] = true;\n map[nums[j]] = true;\n }\n else if (!map.contains(key))\n {\n map[nums[j]] = false;\n }\n }\n map.clear();\n }\n return res;\n }\n};",
"memory": "321930"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> targetSum(vector<int>& nums, int i, int n, int target) {\n vector<int> ans;\n while (i<n){\n int sum = nums[i]+nums[n];\n if(sum==target){\n ans.push_back(nums[i]);\n ans.push_back(nums[n]);\n }\n if(sum<target) i++;\n else n--;\n }\n return ans;\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n map<pair<int,pair<int,int>>, int>mp;\n int n= nums.size()-1;\n vector<vector<int>>vec;\n for(int i=0;i<n-1;i++)\n {\n vector<int> t = targetSum(nums, i+1, n, 0-nums[i]);\n int j=0;\n int s= t.size();\n while(j<s){\n if(!mp.contains({nums[i],{t[j],t[j+1]}})) {\n vector<int> ans;\n ans.push_back(t[j]);\n ans.push_back(t[j+1]);\n ans.push_back(nums[i]);\n mp[{nums[i],{t[j],t[j+1]}}]+=1;\n vec.push_back(ans);\n }\n j+=2;\n }\n }\n return vec;\n }\n};",
"memory": "326841"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n set<vector<int>> ans;\n\n sort(nums.begin(), nums.end());\n int zero_idx = lower_bound(nums.begin(), nums.end(), 0) - nums.begin();\n int pos_idx = upper_bound(nums.begin(), nums.end(), 0) - nums.begin();\n\n // all zeros\n if (pos_idx - zero_idx >= 3) {\n ans.insert({0,0,0});\n }\n\n // one zero, one positive, one negative\n if (pos_idx - zero_idx >= 1) {\n unordered_set<int> negatives;\n for (int i = 0; i < zero_idx; i++) {\n negatives.insert(nums[i]);\n }\n for (int i = pos_idx; i < n; i++) {\n if (negatives.find(-nums[i]) != negatives.end()) {\n ans.insert({-nums[i],0,nums[i]});\n }\n }\n }\n\n // two negatives, one positive\n unordered_map<int, vector<pair<int,int>>> negsums;\n for (int i = 0; i < zero_idx; i++) {\n for (int j = i+1; j < zero_idx; j++) {\n negsums[nums[i]+nums[j]].push_back({nums[i],nums[j]});\n }\n }\n for (int i = pos_idx; i < n; i++) {\n if (negsums.find(-nums[i])!=negsums.end()) {\n for (auto& p : negsums[-nums[i]]) {\n ans.insert({p.first, p.second, nums[i]});\n }\n }\n }\n\n // two positives, one negative\n unordered_map<int, vector<pair<int,int>>> possums;\n for (int i = pos_idx; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n possums[nums[i]+nums[j]].push_back({nums[i],nums[j]});\n }\n }\n for (int i = 0; i < zero_idx; i++) {\n if (possums.find(-nums[i])!=possums.end()) {\n for (auto& p : possums[-nums[i]]) {\n ans.insert({p.first, p.second, nums[i]});\n }\n }\n }\n\n vector<vector<int>> vecans;\n for (auto& a : ans) {\n vecans.push_back(a);\n }\n return vecans;\n\n }\n};",
"memory": "331753"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n set<vector<int>> ans;\n\n sort(nums.begin(), nums.end());\n int zero_idx = lower_bound(nums.begin(), nums.end(), 0) - nums.begin();\n int pos_idx = upper_bound(nums.begin(), nums.end(), 0) - nums.begin();\n\n // all zeros\n if (pos_idx - zero_idx >= 3) {\n ans.insert({0,0,0});\n }\n\n // one zero, one positive, one negative\n if (pos_idx - zero_idx >= 1) {\n unordered_set<int> negatives;\n for (int i = 0; i < zero_idx; i++) {\n negatives.insert(nums[i]);\n }\n for (int i = pos_idx; i < n; i++) {\n if (negatives.find(-nums[i]) != negatives.end()) {\n ans.insert({-nums[i],0,nums[i]});\n }\n }\n }\n\n // two negatives, one positive\n unordered_map<int, vector<pair<int,int>>> negsums;\n for (int i = 0; i < zero_idx; i++) {\n for (int j = i+1; j < zero_idx; j++) {\n negsums[nums[i]+nums[j]].push_back({nums[i],nums[j]});\n }\n }\n for (int i = pos_idx; i < n; i++) {\n if (negsums.find(-nums[i])!=negsums.end()) {\n for (auto& p : negsums[-nums[i]]) {\n ans.insert({p.first, p.second, nums[i]});\n }\n }\n }\n\n // two positives, one negative\n unordered_map<int, vector<pair<int,int>>> possums;\n for (int i = pos_idx; i < n; i++) {\n for (int j = i+1; j < n; j++) {\n possums[nums[i]+nums[j]].push_back({nums[i],nums[j]});\n }\n }\n for (int i = 0; i < zero_idx; i++) {\n if (possums.find(-nums[i])!=possums.end()) {\n for (auto& p : possums[-nums[i]]) {\n ans.insert({p.first, p.second, nums[i]});\n }\n }\n }\n\n vector<vector<int>> vecans;\n for (auto& a : ans) {\n vecans.push_back(a);\n }\n return vecans;\n\n }\n};",
"memory": "336664"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<pair<int, int>> twoSum(vector<int> &nums, int target, int start, map<int, int> &mp)\n {\n vector<pair<int, int>> ans;\n for(int i=start; i<nums.size(); i++)\n {\n if(mp.find(target - nums[i]) != mp.end() && mp[target - nums[i]] > i)\n {\n ans.push_back({i, mp[target - nums[i]]});\n }\n }\n return ans;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n map<int, int> mp;\n for(int i=0; i<nums.size(); i++)\n {\n mp[nums[i]] = i;\n }\n set<vector<int>> ans;\n for(int i=0; i<nums.size(); i++)\n {\n vector<pair<int, int>> v = twoSum(nums, -nums[i], i+1, mp);\n vector<int> x;\n for(auto elem: v)\n {\n x = {nums[i], nums[elem.first], nums[elem.second]};\n sort(x.begin(), x.end());\n ans.insert(x);\n }\n }\n vector<vector<int>> ret;\n for(auto elem: ans)\n {\n ret.push_back(elem);\n }\n return ret;\n }\n};",
"memory": "336664"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void twoSum(vector<vector<int>> & ans, vector<int> & v, int r, int tgt) {\n if (tgt < v[0] + v[1] || v[r - 1] + v[r] < tgt) return;\n unordered_map<int, int> mp;\n for (int i = r; i >= 0; --i) {\n mp[v[i]] = i;\n }\n int pre = INT_MAX;\n for (int i = r; i >= 0; --i) {\n if (v[i] == pre) continue;\n int x = tgt - v[i];\n if (mp.find(x) != mp.end() && mp[x] < i) {\n ans.push_back({x, v[i], v[r + 1]});\n }\n pre = v[i];\n }\n return;\n }\n \n vector<vector<int>> threeSum(vector<int>& v) {\n vector<vector<int>> ans;\n int n = v.size();\n sort(v.begin(), v.end());\n int pre = INT_MAX;\n for (int i = n - 1; i >= 2; --i) {\n if (v[i] == pre) continue;\n twoSum(ans, v, i - 1, -v[i]);\n pre = v[i];\n }\n return ans;\n }\n};",
"memory": "341575"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void print(vector<int> &nums)\n {\n cout << \"[ \" << nums[0] << \", \";\n for (int i = 1 ; i < nums.size() ; i++)\n {\n cout << nums[i] << \", \";\n }\n\n cout << \" ]\" << endl;\n }\n vector<vector<int>> threeSum(vector<int>& nums) \n {\n sort(nums.begin(), nums.end());\n int iter = 0;\n vector<vector<int>> out;\n print(nums);\n unordered_map<int,vector<unordered_set<int>>> seen;\n while (iter < nums.size() && nums[iter] <= 0)\n {\n if (seen.find(nums[iter]) == seen.end())\n {\n seen[nums[iter]] = vector(2, unordered_set<int>());\n vector<vector<int>> triplet = twoNums(nums, iter+1, -nums[iter], seen[nums[iter]]);\n if (!triplet.empty())\n out.insert(out.end(), triplet.begin(), triplet.end());\n }\n iter++;\n }\n\n return out;\n }\n\n vector<vector<int>> twoNums(vector<int>& nums, int start, int target, vector<unordered_set<int>> &seen)\n {\n int n = nums.size()-1;\n vector<vector<int>> out;\n for (int i = start ; i < nums.size() - 1; i++)\n {\n int num1 = nums[i];\n int num2 = bs(i+1, n, nums, num1, target);\n vector<int> triplet = {-target, num1, num2};\n if (num1 + num2 == target && unique({num1, num2}, seen))\n {\n out.push_back(triplet);\n }\n }\n\n return out;\n }\n\n int bs(int start, int end, vector<int> &nums, int num1, int target)\n {\n int mid = start + (end - start)/2;\n //cout << \" Performing Binary Search : target = -\" <<target<< \", num1 = \" << num1 <<endl;\n while (start < end)\n {\n mid = start + (end - start)/2;\n if (nums[mid] + num1 > target)\n {\n end = mid - 1;\n }\n\n else if (nums[mid] + num1 < target)\n {\n start = mid + 1;\n }\n\n else\n { \n //cout << \" - num2 = \" << nums[mid] << endl;\n return nums[mid];\n }\n\n }\n\n //cout << \" - num2 = \" << nums[start] << endl;\n return nums[start];\n }\n\n bool unique(vector<int> triplet, vector<unordered_set<int>> &seen)\n {\n bool ret = false;\n for (int i = 0 ; i < seen.size() ; i++)\n {\n if (seen[i].find(triplet[i]) == seen[i].end())\n {\n seen[i].insert(triplet[i]);\n ret = true;\n }\n }\n\n return ret;\n }\n};",
"memory": "346486"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n void print(vector<int> &nums)\n {\n cout << \"[ \" << nums[0] << \", \";\n for (int i = 1 ; i < nums.size() ; i++)\n {\n cout << nums[i] << \", \";\n }\n\n cout << \" ]\" << endl;\n }\n vector<vector<int>> threeSum(vector<int>& nums) \n {\n sort(nums.begin(), nums.end());\n int iter = 0;\n vector<vector<int>> out;\n unordered_map<int,vector<unordered_set<int>>> seen;\n while (iter < nums.size() && nums[iter] <= 0)\n {\n if (seen.find(nums[iter]) == seen.end())\n {\n seen[nums[iter]] = vector(2, unordered_set<int>());\n vector<vector<int>> triplet = twoNums(nums, iter+1, -nums[iter], seen[nums[iter]]);\n if (!triplet.empty())\n out.insert(out.end(), triplet.begin(), triplet.end());\n }\n iter++;\n }\n\n return out;\n }\n\n vector<vector<int>> twoNums(vector<int>& nums, int start, int target, vector<unordered_set<int>> &seen)\n {\n int n = nums.size()-1;\n vector<vector<int>> out;\n for (int i = start ; i < nums.size() - 1; i++)\n {\n int num1 = nums[i];\n int num2 = bs(i+1, n, nums, num1, target);\n vector<int> triplet = {-target, num1, num2};\n if (num1 + num2 == target && unique({num1, num2}, seen))\n {\n out.push_back(triplet);\n }\n }\n\n return out;\n }\n\n int bs(int start, int end, vector<int> &nums, int num1, int target)\n {\n int mid = start + (end - start)/2;\n //cout << \" Performing Binary Search : target = -\" <<target<< \", num1 = \" << num1 <<endl;\n while (start < end)\n {\n mid = start + (end - start)/2;\n if (nums[mid] + num1 > target)\n {\n end = mid - 1;\n }\n\n else if (nums[mid] + num1 < target)\n {\n start = mid + 1;\n }\n\n else\n { \n //cout << \" - num2 = \" << nums[mid] << endl;\n return nums[mid];\n }\n\n }\n\n //cout << \" - num2 = \" << nums[start] << endl;\n return nums[start];\n }\n\n bool unique(vector<int> triplet, vector<unordered_set<int>> &seen)\n {\n bool ret = false;\n for (int i = 0 ; i < seen.size() ; i++)\n {\n if (seen[i].find(triplet[i]) == seen[i].end())\n {\n seen[i].insert(triplet[i]);\n ret = true;\n }\n }\n\n return ret;\n }\n};",
"memory": "346486"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "void f(int &i,const int &n,const vector<int>& nums)\n{\n while(i<n&&nums[i]==nums[i-1])\n i++;\n}\nclass Solution {\npublic:\nvector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>>niz;\n int i=0,j,k=3,n,s,n1;\n n=nums.size();\n pair<int,int>x;\n n=nums.size(); \n sort(nums.begin(),nums.end());\n n1=upper_bound(nums.begin(),nums.end(),0)-nums.begin();\n map<int,vector<int>>mapa;\n for(int i=0;i<n1;i++,f(i,n,nums))\n for(int j=i+1;j<n;j++,f(j,n,nums))\n {\n s=-nums[i]-nums[j];\n mapa[s].push_back(i);\n mapa[s].push_back(j);\n }\n if(nums[0]!=0||nums[n-1]!=0||n<3000) \n for(int i=max(n1-3,0);i<n;i++,f(i,n,nums))\n {\n s=nums[i];\n while(i<n-1&&nums[i]==nums[i+1])\n i++;\n for(int j=0;j<mapa[nums[i]].size();j+=2)\n if(i>mapa[s][j]&&i>mapa[s][j+1])\n niz.push_back({nums[mapa[s][j]],nums[mapa[s][j+1]],nums[i]});\n }\n else\n niz.push_back({0,0,0});\n return niz;\n }\n};",
"memory": "351398"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<int> get_sorted(vector<int> nums, int start, int end)\n {\n\n vector<int> temp, first, second;\n temp.clear();\n if(start==end)\n {\n temp.push_back(nums[start]);\n return temp;\n \n }\n\n int mid = (start + end)/2;\n\n first = get_sorted(nums, start, mid);\n\n second = get_sorted(nums, mid+1, end);\n\n int i=0,j=0;\n temp.clear();\n\n while(i<first.size() || j< second.size())\n {\n if(i>=first.size() && j< second.size())\n {\n temp.push_back(second[j]);\n j++;\n continue;\n }\n\n if(j>=second.size() && i<first.size())\n {\n temp.push_back(first[i]);\n i++;\n continue;\n }\n\n if(first[i]<=second[j])\n {\n temp.push_back(first[i]);\n i++;\n }\n else\n {\n temp.push_back(second[j]);\n j++;\n }\n\n }\n\n return temp;\n\n\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n\n vector<int> sorted_num;\n vector< vector<int> > sol;\n\n sorted_num = get_sorted(nums, 0, nums.size()-1);\n\n /* for(int k=0;k<sorted_num.size();k++)\n {\n cout << sorted_num[k] << endl;\n }*/\n\n int start=0, end = sorted_num.size()-1, sum=0, temp=0, zc=0;\n\n for(int g=0;g<sorted_num.size();g++)\n {\n\n if(sorted_num[g]==0)\n {\n zc++;\n\n }\n }\n\n for(int i=0; i<sorted_num.size(); i++)\n {\n if(sorted_num[i]>=0 && zc<3)\n {\n continue;\n }\n\n if((i-1)>=0 && (sorted_num[i]==sorted_num[i-1]))\n {\n //cout << \"skip \" << i << \" \" << sorted_num[i] << endl; \n continue;\n }\n //cout << \" at \" << i << sorted_num[i] << endl;\n sum = sorted_num[i]*(-1);\n start=i+1, end = sorted_num.size()-1;\n\n while(start<end)\n {\n if(start==i)\n {\n start++;\n continue;\n }\n\n if(end==i)\n {\n end--;\n continue;\n\n }\n temp = sorted_num[start] + sorted_num[end];\n // cout << \"temp \" << temp << endl;\n\n /*\n if(temp==sum)\n {\n cout << \"found m at \" << sorted_num[start] << sorted_num[end] << endl;\n int rf=start, rs=end;\n\n int fc = 1, sc = 1;\n\n while((rf<rs) && ((rf+1)<sorted_num.size()) && ((rs-1)>0))\n {\n if(sorted_num[rf]==sorted_num[rf+1])\n {\n fc = fc + 1;\n rf++;\n \n }\n\n if(sorted_num[rs]==sorted_num[rs-1])\n {\n sc = sc + 1;\n rs--;\n\n }\n }\n\n int comb = fc*sc;\n \n for(int l=0; l<comb;l++)\n {\n vector<int> ch;\n ch.push_back(i);\n ch.push_back(sorted_num[start]);\n ch.push_back(sorted_num[end]);\n\n sol.push_back(ch);\n }\n\n start = rf;\n end = rs;\n\n\n \n }*/\n\n\n if(temp==sum)\n {\n // cout << \"found m \" << endl << temp;\n vector<int> ch;\n ch.push_back(sorted_num[i]);\n ch.push_back(sorted_num[start]);\n ch.push_back(sorted_num[end]);\n\n sol.push_back(ch);\n\n while( (start+1) < sorted_num.size())\n {\n // cout << \"s \" << start << endl; \n if(sorted_num[start+1]==sorted_num[start])\n {\n start++;\n }\n else\n {\n break;\n }\n }\n\n start++;\n \n\n while((end-1)>0)\n {\n if(sorted_num[end-1]==sorted_num[end])\n {\n end--;\n }\n else\n {\n break;\n }\n\n }\n end--;\n \n //start++;\n\n }\n\n if(temp>sum)\n {\n end--;\n }\n\n if(temp<sum)\n {\n start++;\n }\n\n }\n\n \n\n }\n\n\n return sol;\n\n \n }\n};",
"memory": "351398"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n\n vector<vector<pair<int, int>>> sum12 (2e5+1);\n vector<int> sum3 (2e5+1, 0);\n int n = nums.size();\n for (int i = 0; i < n; i++) {\n if (i > 0 && nums[i] == nums[i-1]) continue;\n for (int j = i+1; j < n; j++) {\n if (nums[j] == nums[j-1] && j != i+1) continue;\n if ((nums[i] <= 0 && nums[j] >= 0) || (nums[i] >= 0 && nums[j] <= 0)) {\n continue;\n }\n if (nums[i] + nums[j] > 1e5 || nums[i] + nums[j] < -1e5) {\n continue;\n } else {\n sum12[nums[i] + nums[j] + 1e5].push_back({nums[i], nums[j]});\n }\n }\n }\n\n vector<vector<int>> out;\n\n for (int i = 0; i < n; i++) {\n sum3[nums[i] + 1e5]++;\n }\n for (int i = 0; i <= 2e5; i++) {\n for (pair<int, int> p : sum12[i]) {\n if (sum3[2e5-i] > 0) {\n // cout << sum3[2e5-i];\n vector<int> temp = {p.first, p.second, 100000-i};\n out.push_back(temp);\n }\n }\n }\n\n if (sum3[0 + 1e5] >= 3) {\n out.push_back({0, 0, 0});\n }\n\n if (sum3[0 + 1e5] >= 1) {\n for (int i = 0; i < 1e5; i++) {\n if (sum3[i] > 0 && sum3[2e5-i] > 0) {\n out.push_back({ i-(int)1e5, (int)1e5 - i, 0});\n }\n }\n }\n\n return out;\n }\n};",
"memory": "356309"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> twosum(vector<int>&nums, int cur) {\n int n = nums.size();\n int st = cur + 1;\n int en = n - 1;\n int target = -nums[cur];\n vector<vector<int>> resAll;\n while(st < en) {\n if(nums[st] + nums[en] < target) {\n st++;\n }\n else if(nums[st] + nums[en] > target) {\n en--;\n }\n else {\n vector<int> res;\n res.push_back(nums[st]);\n res.push_back(nums[en]);\n res.push_back(nums[cur]);\n resAll.push_back(res);\n // break;\n while(st < n - 1 && nums[st] == nums[st + 1]) st++;\n while(en > 0 && nums[en] == nums[en - 1]) en--; \n st++;en--;\n\n }\n }\n return resAll;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n vector<vector<int>> resAll;\n int n = nums.size();\n for(int i = 0; i < n; i++) {\n if(i > 0 && nums[i] == nums[i - 1]) continue;\n vector<vector<int>> res = twosum(nums, i);\n if(!res.empty()) {\n // res.push_back(nums[i]);\n int in = resAll.size();\n \n // if(!resAll.empty()){\n // in--;\n // cout << resAll[in][0];\n // cout << resAll[in][1];\n // cout << resAll[in][2];\n // if(resAll[in][0] != res[0] && resAll[in][1] != res[1])\n // resAll.push_back(res);\n // }\n // else \n resAll.reserve(resAll.size() + res.size());\n resAll.insert( resAll.end(), res.begin(), res.end());\n // resAll.push_back(res);\n }\n }\n return resAll;\n\n }\n};",
"memory": "361220"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n map<int, int> mp;\n mp[0 - nums[0]] = 0;\n set<set<int>> se;\n int a, b, c, value;\n set<int> tmp;\n vector<vector<int>> res;\n int length = nums.size();\n if(length > 2001 && nums[2000] == 0) return {{0,0,0}};\n for(int i = 1;i < length - 1; ++i) {\n for(int j = i + 1;j < length; ++j) {\n value = nums[j] + nums[i];\n if(mp.count(value)) {\n a = nums[mp[value]];\n b = nums[i];\n c = nums[j];\n tmp.clear();\n tmp.insert(a);\n tmp.insert(b);\n tmp.insert(c);\n if(se.count(tmp)) {\n continue;\n }\n se.insert(tmp);\n res.push_back({a, b, c});\n }\n }\n mp[0 - nums[i]] = i;\n }\n return res;\n }\n};",
"memory": "361220"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> ans;\n sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size() - 2; i++) {\n int l = i;\n int m = l + 1;\n int r = nums.size() - 1;\n if (nums[r] == nums[l] && r - l > 2) {\n continue;\n }\n while(m < r) {\n int sum = nums[l] + nums[m] + nums[r];\n if (sum == 0) {\n vector<int> temp{nums[l], nums[m], nums[r]};\n ans.emplace(temp);\n m++;\n } else if (sum > 0) {\n r--;\n } else {\n m++;\n }\n }\n }\n vector<vector<int>> res(ans.begin(), ans.end());\n\n return res;\n }\n};\n",
"memory": "366131"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<vector<int>> ans;\n sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size() - 2; i++) {\n int l = i;\n int m = l + 1;\n int r = nums.size() - 1;\n if (nums[r] == nums[l] && r - l > 2) {\n i = r - 3;\n continue;\n }\n while(m < r) {\n int sum = nums[l] + nums[m] + nums[r];\n if (sum == 0) {\n vector<int> temp{nums[l], nums[m], nums[r]};\n ans.emplace(temp);\n m++;\n } else if (sum > 0) {\n r--;\n } else {\n m++;\n }\n }\n }\n vector<vector<int>> res(ans.begin(), ans.end());\n\n return res;\n }\n};\n",
"memory": "366131"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "// Jai shree ram\n// Hash function for vector<int>\nstruct VectorHash {\n size_t operator()(const vector<int>& v) const {\n size_t hash_value = 0;\n for (int i : v) {\n hash_value ^= hash<int>()(i) + 0x9e3779b9 + (hash_value << 6) + (hash_value >> 2);\n }\n return hash_value;\n }\n};\n\nmap<vector<int>, vector<vector<int>>> pre;\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& v) {\n int n = v.size();\n sort(v.begin(), v.end());\n if(pre.find(v) != pre.end())\n {\n return pre[v];\n }\n\n unordered_map<int, int> mp;\n for (int it : v) mp[it]++;\n\n set<vector<int>> s;\n \n\n for (int i = 0; i < n; i++) {\n for (int j = i + 1; j < n; j++) {\n int req = 0 - (v[i] + v[j]);\n if (req < 0) break;\n mp[v[i]]--;\n mp[v[j]]--;\n if (mp[req] > 0) {\n vector<int> te = {v[i], v[j], req};\n sort(te.begin(), te.end());\n s.insert(te);\n }\n mp[v[i]]++;\n mp[v[j]]++;\n }\n }\n\n vector<vector<int>> ans(s.begin(), s.end()); \n pre[v] = ans;\n return ans;\n\n }\n};",
"memory": "371043"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end()); //NlogN\n\n int len = nums.size();\n\n set<vector<int>> ans;\n\n unordered_map<int, int> m; \n for(auto &x: nums) m[x]++; \n\n for(int i = 0; i < len - 2; i++) {\n if(nums[i] > 0) break;\n if(nums[i] == 0) {\n if(m[0] >= 3) ans.insert({0, 0, 0});\n break;\n }\n //Reduce used index count:\n if(m[nums[i]] == 1) m.erase(nums[i]);\n else m[nums[i]]--;\n \n for(int j = i + 1; j < len - 1; j++) {\n int toFind = -1 * (nums[i] + nums[j]);\n\n if(m[nums[j]] == 1) m.erase(nums[j]);\n else m[nums[j]]--;\n\n if(toFind >= nums[j] && m.find(toFind) != m.end()) {\n ans.insert({nums[i], nums[j], toFind});\n }\n\n m[nums[j]]++;\n }\n }\n\n vector<vector<int>> finalAns(ans.begin(), ans.end());\n\n return finalAns; //O(N^2.logN)\n }\n};",
"memory": "375954"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<vector<int>> ans;\n int mid=0;\n while (mid<nums.size()&&nums[mid]<0)mid++;\n if((mid+2)<nums.size()&&nums[mid]==0&&nums[mid+1]==0&&nums[mid+2]==0) ans.push_back({0,0,0});\n set<int> positive;\n set<int> negative;\n for (int i =0;i<mid;i++) {\n negative.insert(nums[i]);\n }\n for (int i =mid;i<nums.size();i++) {\n positive.insert(nums[i]);\n }\n set<pair<int,int>> check;\n for (int i =0;i<mid;i++) {\n for (int j=i+1;j<mid;j++) {\n if (check.contains({nums[i],nums[j]})) continue;\n check.insert({nums[i],nums[j]});\n if (positive.contains((nums[i]+nums[j])*-1)) {\n ans.push_back({nums[i],nums[j],(nums[i]+nums[j])*-1});\n }\n }\n }\n for (int i =mid;i<nums.size();i++) {\n for (int j=i+1;j<nums.size();j++) {\n if (check.contains({nums[i],nums[j]})) continue;\n check.insert({nums[i],nums[j]});\n if (negative.contains((nums[i]+nums[j])*-1)) {\n ans.push_back({nums[i],nums[j],(nums[i]+nums[j])*-1});\n }\n }\n }\n return ans;\n }\n};",
"memory": "375954"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>>ans;\n bool flag=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]!=0)\n flag=1;\n }\n if(flag==0)\n {\n vector<int>temp1;\n temp1.push_back(0);\n temp1.push_back(0);\n temp1.push_back(0);\n ans.push_back(temp1);\n return ans;\n }\n sort(nums.begin(),nums.end());\n int len=nums.size();\n for(int i=0;i<len-2;i++)\n {\n int j=i+1,k=len-1;\n while(j<k)\n {\n if(nums[i]+nums[j]+nums[k]<0)\n j++;\n else if(nums[i]+nums[j]+nums[k]>0)\n k--;\n else\n {\n vector<int>temp;\n temp.push_back(nums[i]);\n temp.push_back(nums[j]);\n temp.push_back(nums[k]);\n ans.push_back(temp);\n j++;\n k--;\n }\n }\n }\n set<vector<int>>s;\n for(int i=0;i<ans.size();i++)\n s.insert(ans[i]);\n vector<vector<int>>ans2;\n auto it=s.begin();\n for(int j=0;j<s.size();j++)\n {\n ans2.push_back(*it);\n it++;\n }\n return ans2;\n }\n};",
"memory": "380865"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> result;\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n for(int i=0;i<n;i++){\n int target = -nums[i];\n int left = i+1;\n int right = n-1;\n\n while(left<right){\n \n if(nums[left] + nums[right] > target)\n {\n right--;\n continue;\n }\n\n if(nums[left] + nums[right] < target)\n {\n left++;\n continue;\n }\n\n if(nums[left] + nums[right] == target)\n {\n result.push_back({nums[i], nums[left], nums[right]});\n \n if(nums[right]== nums[right-1])\n right--;\n\n if(nums[left] == nums[left+1])\n left++;\n \n right--;\n left++;\n }\n\n }\n\n }\n sort(result.begin(), result.end());\n\n set<vector<int>> unique(result.begin(), result.end());\n return vector<vector<int>>(unique.begin(), unique.end());\n }\n};",
"memory": "380865"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> answer;\n std::map<int, int> locations;\n std::map<vector<int>, int> test;\n std::sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size(); i++) {\n locations[nums[i]] = i;\n }\n if (locations.size() == 1 && nums[0] == 0) {\n vector<int> triplet = {0, 0, 0}; \n answer.emplace_back(triplet); \n return answer;\n }\n for (int i = 0; i < nums.size() - 2; i++) {\n for (int j = i + 1; j < nums.size() - 1; j++) {\n const int pre_sum = -(nums[i] + nums[j]);\n const auto it = locations.find(pre_sum);\n if (it != locations.end()) {\n const auto index = it->second;\n if (index == i || index == j) {\n continue;\n }\n vector<int> triplet;\n if (pre_sum <= nums[i]) {\n triplet = {pre_sum, nums[i], nums[j]};\n } else if (pre_sum >= nums[j]) {\n triplet = {nums[i], nums[j], pre_sum}; \n } else {\n triplet = {nums[i], pre_sum, nums[j]}; \n }\n \n if (test.count({triplet[0], triplet[1]}) == 0) {\n answer.emplace_back(triplet); \n test[{triplet[0], triplet[1]}] = 0;\n }\n } \n }\n }\n \n return answer;\n }\n};",
"memory": "385776"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> answer;\n std::map<int, int> locations;\n std::map<vector<int>, int> test;\n std::sort(nums.begin(), nums.end());\n for (int i = 0; i < nums.size(); i++) {\n locations[nums[i]] = i;\n }\n if (locations.size() == 1 && nums[0] == 0) {\n vector<int> triplet = {0, 0, 0}; \n answer.emplace_back(triplet); \n return answer;\n }\n for (int i = 0; i < nums.size() - 2; i++) {\n for (int j = i + 1; j < nums.size() - 1; j++) {\n const int pre_sum = -(nums[i] + nums[j]);\n const auto it = locations.find(pre_sum);\n if (it != locations.end()) {\n const auto index = it->second;\n if (index == i || index == j) {\n continue;\n }\n vector<int> triplet;\n if (pre_sum <= nums[i]) {\n triplet = {pre_sum, nums[i], nums[j]};\n } else if (pre_sum >= nums[j]) {\n triplet = {nums[i], nums[j], pre_sum}; \n } else {\n triplet = {nums[i], pre_sum, nums[j]}; \n }\n \n if (test.count({triplet[0], triplet[1]}) == 0) {\n answer.emplace_back(triplet); \n test[{triplet[0], triplet[1]}] = 0;\n }\n } \n }\n }\n \n return answer;\n }\n};",
"memory": "385776"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n map<int, int> mp;\n mp[0 - nums[0]] = 0;\n set<set<int>> se;\n int a, b, c, value;\n set<int> tmp;\n vector<vector<int>> res;\n int length = nums.size();\n if(length > 2001 && nums[2000] == 0) return {{0,0,0}};\n for(int i = 1;i < length - 1; ++i) {\n for(int j = i + 1;j < length; ++j) {\n value = nums[j] + nums[i];\n if(mp.count(value)) {\n a = nums[mp[value]];\n b = nums[i];\n c = nums[j];\n tmp.clear();\n tmp.insert(a);\n tmp.insert(b);\n tmp.insert(c);\n if(se.count(tmp)) {\n continue;\n }\n se.insert(tmp);\n res.push_back({a, b, c});\n }\n }\n mp[0 - nums[i]] = i;\n }\n return res;\n }\n};",
"memory": "390688"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> twoSum(vector<int>& nums, int left, int target){\n int right=nums.size()-1;\n vector<vector<int>> vpp;\n while(left<right){\n if(nums[left]+nums[right]== target){\n vpp.push_back({nums[left],nums[right]});\n left++;\n right--;\n }\n else if(nums[left]+nums[right]>target){\n right--;\n }\n else{\n left++;\n }\n }\n return vpp;\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n for(int i=0;i<nums.size();i++){\n if(nums[i]!=0){\n break;\n }\n else if(i==nums.size()-1){\n return {{0,0,0}};\n }\n } \n vector<vector<int>> vpp;\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size();i++){\n vector<vector<int>> vp = twoSum(nums,i+1,0-nums[i]);\n if(vp.size()!=0){\n for(int k=0;k<vp.size();k++){\n vp[k].push_back(nums[i]);\n vpp.push_back(vp[k]);\n }\n }\n }\n sort(vpp.begin(),vpp.end());\n vpp.erase(unique(vpp.begin(),vpp.end()),vpp.end());\n return vpp;\n\n\n \n }\n};",
"memory": "390688"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n set<tuple<int, int, int>> res;\n sort(nums.begin(), nums.end());\n int n = nums.size();\n for (int i = 0; i < n; ++i) {\n int j = i + 1;\n int k = n - 1;\n int target = -nums[i];\n while (j < k) {\n int sum = nums[j] + nums[k];\n if (sum == target) {\n int tup[3] = {nums[i], nums[j], nums[k]};\n sort(tup, tup + 3);\n res.emplace(tup[0], tup[1], tup[2]);\n ++j;\n --k;\n } else if (sum < target) {\n ++j;\n } else {\n --k;\n }\n }\n }\n vector<vector<int>> vec;\n for (auto &&t : res) {\n vec.push_back(vector<int>{std::get<0>(t),std::get<1>(t),std::get<2>(t)});\n }\n return vec;\n }\n};",
"memory": "395599"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "struct Node{\n int val;\n int i;\n int j;\n Node(int val,int i,int j): val(val),i(i),j(j) {};\n // Define the operator< to compare Node objects\n bool operator<(const Node& other) const {\n if (val != other.val) return val < other.val;\n if (i != other.i) return i < other.i;\n return j < other.j;\n }\n};\n\nstd::ostream& operator<<(std::ostream& os, const Node& node) {\n os << \"Node(val=\" << node.val << \", i=\" << node.i << \", j=\" << node.j << \")\";\n return os;\n}\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n // vector<int> nums;\n // map<int,int> c;\n // for(auto x:p){\n // if(c[x]>3||(x!=0&&c[x]>2))continue;\n // c[x]++;\n // nums.push_back(x);\n // }\n sort(nums.begin(),nums.end());\n // for(auto x:nums)cout <<x << \" \";cout<<endl;\n // map<int,int> mp;\n // for(int i=0;i<nums.size();i++)mp[nums[i]]=i;\n set<Node> q;\n q.insert(Node(nums[0]+nums[1],0,1));\n for(int i=1;i<nums.size()-1;i++){\n if(nums[i-1]!=nums[i])q.insert(Node(nums[i]+nums[i+1],i,i+1));\n }\n int k = nums.size()-1;\n vector<vector<int>> ans;\n while(q.size()!=0){\n // for (const auto& node : q) {\n // std::cout << node << std::endl;\n // }\n Node node = *q.begin();\n int c = - node.val;\n int b = nums[node.j], a = nums[node.i],i=node.i,j=node.j;\n // cout << a << \" \" << b << \" \" << c << endl;\n q.erase(q.begin());\n while(k>-1&&nums[k]>c)k--;\n if(k<0)break;\n // cout << k <<endl;\n if(nums[k]==c){\n // cout << i << \"---\" << j << \"---\" << k <<endl;\n if(j<k)ans.push_back({a,b,c});\n }\n while(j+1<nums.size()&&nums[j+1]==nums[j])j++;\n if(j+1<nums.size()) q.insert(Node(nums[i]+nums[j+1],i,j+1));\n }\n return ans;\n }\n};\n",
"memory": "400510"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "struct Node{\n int val;\n int i;\n int j;\n Node(int val,int i,int j): val(val),i(i),j(j) {};\n // Define the operator< to compare Node objects\n bool operator<(const Node& other) const {\n if (val != other.val) return val < other.val;\n if (i != other.i) return i < other.i;\n return j < other.j;\n }\n};\n\nstd::ostream& operator<<(std::ostream& os, const Node& node) {\n os << \"Node(val=\" << node.val << \", i=\" << node.i << \", j=\" << node.j << \")\";\n return os;\n}\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n // vector<int> nums;\n // map<int,int> c;\n // for(auto x:p){\n // if(c[x]>3||(x!=0&&c[x]>2))continue;\n // c[x]++;\n // nums.push_back(x);\n // }\n sort(nums.begin(),nums.end());\n // for(auto x:nums)cout <<x << \" \";cout<<endl;\n // map<int,int> mp;\n // for(int i=0;i<nums.size();i++)mp[nums[i]]=i;\n set<Node> q;\n q.insert(Node(nums[0]+nums[1],0,1));\n for(int i=1;i<nums.size()-1;i++){\n if(nums[i-1]!=nums[i])q.insert(Node(nums[i]+nums[i+1],i,i+1));\n }\n int k = nums.size()-1;\n vector<vector<int>> ans;\n while(q.size()!=0){\n // for (const auto& node : q) {\n // std::cout << node << std::endl;\n // }\n Node node = *q.begin();\n int c = - node.val;\n int b = nums[node.j], a = nums[node.i],i=node.i,j=node.j;\n // cout << a << \" \" << b << \" \" << c << endl;\n q.erase(q.begin());\n while(k>-1&&nums[k]>c)k--;\n if(k<0)break;\n // cout << k <<endl;\n if(nums[k]==c){\n // cout << i << \"---\" << j << \"---\" << k <<endl;\n if(j<k)ans.push_back({a,b,c});\n }\n while(j+1<nums.size()&&nums[j+1]==nums[j])j++;\n if(j+1<nums.size()) q.insert(Node(nums[i]+nums[j+1],i,j+1));\n }\n return ans;\n }\n};\n",
"memory": "400510"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\n#define f(i, a, b) for (int i = a; i < b; i++)\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n \n int i, j, k, n = nums.size(), diff;\n sort(nums.begin(), nums.end());\n vector <vector <int>> ans;\n vector <int> temp;\n map <pair<int, int>, int> mp;\n\n f(i, 0, n - 2)\n {\n if (i != 0 && nums[i] == nums[i - 1]) continue;\n diff = 0 - nums[i];\n j = i + 1, k = n - 1;\n mp.clear();\n while (j < k)\n {\n if (mp[make_pair(nums[j], nums[k])] > 0) j++, k--;\n else {\n temp.clear();\n if (nums[j] + nums[k] == diff)\n {\n temp.push_back(nums[i]), temp.push_back(nums[j]), temp.push_back(nums[k]);\n ans.push_back(temp);\n mp[make_pair(nums[j], nums[k])]++;\n j++, k--;\n }\n else if (nums[j] + nums[k] < diff) j++;\n else k--;\n }\n }\n }\n return ans;\n }\n};\n",
"memory": "405421"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool bin_search(vector<int>& sorted_arr, int start, int end, int target){\n if (start > end) return false; \n int mid = (start + end) / 2;\n if (sorted_arr[mid] == target){\n return true;\n }\n if (sorted_arr[mid] < target){\n return bin_search(sorted_arr, mid + 1, end, target);\n }else{\n return bin_search(sorted_arr, start, mid - 1, target);\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n set<tuple<int,int,int>> s;\n for (int i = 0; i < nums.size() - 2; i ++){\n for (int j = i + 1; j < nums.size() - 1; j ++){\n int curr_sum = nums[i] + nums[j];\n if (bin_search(nums, j + 1, nums.size() - 1, -curr_sum)){\n s.emplace(make_tuple(nums[i], nums[j], -curr_sum));\n }\n }\n }\n vector<vector<int>> ans;\n for (auto t : s){\n ans.push_back({get<0>(t), get<1>(t), get<2>(t)});\n }\n return ans;\n }\n};",
"memory": "405421"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> ans;\n set<vector<int>> temp;\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size()-2;i++){\n int target = -(nums[i]); \n vector<int> vec(nums.begin()+i+1,nums.end());\n int j = 0;\n int k = vec.size()-1;\n \n while(j<k){\n if(vec[j]+vec[k] == target){\n vector<int>ok={nums[i],vec[j],vec[k]};\n sort(ok.begin(),ok.end());\n temp.insert(ok);\n j++;k--;\n }\n else if(vec[j] + vec[k] < target){\n j++;\n }\n else k--;\n }\n }\n for(auto ch:temp){\n ans.push_back(ch);\n }\n return ans;\n }\n};",
"memory": "410333"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n int i=0;\n vector<vector<int>>sol;\n while(i<arr.size()-2){\n int j=i+1;int k=arr.size()-1;\n while(j<k){\n vector<int>ans(3);\n if(arr[i]+arr[j]+arr[k]==0){\n ans[0]=arr[i];\n ans[1]=arr[j];\n ans[2]=arr[k];\n sol.push_back(ans);\n while(j<k && arr[j]==arr[j+1] ){\n j++;\n }\n j++;\n while(j<k && arr[k]==arr[k-1] ){\n k--;\n }\n k--;\n }\n else if(arr[i]+arr[j]+arr[k]<0){\n while(j<k && arr[j]==arr[j+1] ){\n j++;\n }\n j++;\n }\n else{\n while(j<k && arr[k]==arr[k-1] ){\n k--;\n }\n k--;\n }\n\n }\n while(i<arr.size()-1 && arr[i]==arr[i+1] ){\n i++;\n }\n i++;\n \n }\n return sol;\n\n }\n};",
"memory": "410333"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int pivot = 0;\n int it = 0;\n int target = 0;\n vector<vector<int>> retVec;\n unordered_map<int,int> numMap;\n\n\n sort(nums.begin(), nums.end());\n for(; pivot < nums.size(); ++pivot)\n {\n if(pivot > 0 && nums[pivot] == nums[pivot - 1]) continue;\n target = 0 - nums[pivot];\n numMap.clear();\n for(int it = pivot + 1; it < nums.size(); ++it)\n {\n \n if(numMap.find(nums[it]) != numMap.end())\n {\n retVec.push_back({nums[pivot],nums[it], target-nums[it]});\n while(it + 1 < nums.size() && nums[it] == nums[it + 1]) it++;\n }\n else\n {\n numMap[target-nums[it]] = nums[it];\n }\n } \n }\n\n return retVec;\n }\n};",
"memory": "415244"
} |
15 | <p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>
<p>Notice that the solution set must not contain duplicate triplets.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong>
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 3000</code></li>
<li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int>>ans;\n\n unordered_map<int,int>mp;\n\n sort(nums.begin(),nums.end());\n\n for(int i = 0;i<n;i++)\n {\n if(i>0 and nums[i] == nums[i-1]) continue;\n int sum = -1 * (nums[i]);\n for(int j = i+1;j<n;j++)\n {\n if(mp.find(nums[j])!=mp.end()) {\n if(mp[nums[j]] == 1)\n { \n ans.push_back({nums[i],sum - nums[j],nums[j]});\n mp[nums[j]]++;\n }\n \n }\n else if(mp[sum - nums[j]] == 0) mp[sum - nums[j]]++;\n \n } \n //so that at each iteration new pairs are formed and no old pairs interfere\n mp.clear();\n\n\n }\n return ans;\n }\n};",
"memory": "415244"
} |
16 | <p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p>
<p>Return <em>the sum of the three integers</em>.</p>
<p>You may assume that each input would have exactly one solution.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0], target = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 500</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int threeSumClosest(vector<int>& nums, int target) {\n ranges::sort(nums);\n auto best = nums[0] + nums[1] + nums[2];\n\n for (int i = 0; i < nums.size()-1; i++) {\n int left = i+1;\n int right = nums.size() - 1;\n while (left < right) {\n auto const cur = nums[i] + nums[left] + nums[right];\n if (std::abs(target-best) > std::abs(target-cur)) {\n best = cur; \n }\n if (cur < target) {\n left++;\n } else if (cur > target) {\n right--;\n } else {\n return target;\n }\n }\n }\n return best;\n }\n};",
"memory": "12600"
} |
16 | <p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p>
<p>Return <em>the sum of the three integers</em>.</p>
<p>You may assume that each input would have exactly one solution.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0], target = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 500</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int threeSumClosest(vector<int>& nums, int target) {\n sort(nums.begin(), nums.end());\n int closest_sum = INT_MAX / 2; // A large value but not overflow\n \n for (int i = 0; i < nums.size() - 2; ++i) {\n int left = i + 1, right = nums.size() - 1;\n while (left < right) {\n int current_sum = nums[i] + nums[left] + nums[right];\n if (abs(current_sum - target) < abs(closest_sum - target)) {\n closest_sum = current_sum;\n }\n if (current_sum < target) {\n ++left;\n } else if (current_sum > target) {\n --right;\n } else {\n return current_sum;\n }\n }\n }\n \n return closest_sum;\n }\n};",
"memory": "13200"
} |
16 | <p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>target</code>, find three integers in <code>nums</code> such that the sum is closest to <code>target</code>.</p>
<p>Return <em>the sum of the three integers</em>.</p>
<p>You may assume that each input would have exactly one solution.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,1,-4], target = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [0,0,0], target = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> The sum that is closest to the target is 0. (0 + 0 + 0 = 0).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 <= nums.length <= 500</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int threeSumClosest(vector<int>& nums, int target) {\n sort(nums.begin(), nums.end());\n int closest_sum = INT_MAX / 2; \n \n for (int i = 0; i < nums.size() - 2; ++i) {\n int left = i + 1, right = nums.size() - 1;\n while (left < right) {\n int current_sum = nums[i] + nums[left] + nums[right];\n if (abs(current_sum - target) < abs(closest_sum - target)) {\n closest_sum = current_sum;\n }\n if (current_sum < target) {\n ++left;\n } else if (current_sum > target) {\n --right;\n } else {\n return current_sum;\n }\n }\n }\n \n return closest_sum;\n }\n};",
"memory": "13200"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> output = {\"\"};\n for(char dig: digits){\n vector<char> toAdd;\n switch(dig){\n case '2':\n toAdd = {'a','b','c'};\n break;\n case '3':\n toAdd = {'d','e','f'};\n break;\n case '4':\n toAdd = {'g','h','i'};\n break;\n case '5':\n toAdd = {'j','k','l'};\n break;\n case '6':\n toAdd = {'m','n','o'};\n break;\n case '7':\n toAdd = {'p','q','r','s'};\n break;\n case '8':\n toAdd = {'t','u','v'};\n break;\n case '9':\n toAdd = {'w','x','y','z'};\n break; \n }\n int m = toAdd.size();\n if(m > 0){\n int n = output.size();\n for(int i = 0; i < n; i++){\n string temp = output[i];\n output[i] += toAdd[0];\n for(int j = 1; j < m; j++){\n output.push_back(temp+toAdd[j]);\n }\n }\n }\n }\n if(output[0]== \"\"){return {};}\n return output;\n }\n};",
"memory": "7700"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void recur(string& digits, int idx, string& tmp, vector<string>& ans){\n if(tmp.size() == digits.size()){\n ans.push_back(tmp);\n return;\n }\n //for each digit we can have 3 values, iterate over those\n int digi = digits[idx] - 48;\n int end = 3;\n int start = (int)'a' + (digits[idx] - 50)*3;\n if(digi == 7 || digi == 9){\n end = 4;\n }\n if(digi == 8 || digi == 9) {\n start = (int)'a' + (digits[idx] - 50)*3+1;\n }\n for(char i = 0; i < end; i++){\n tmp.push_back((char)(start+i));\n recur(digits, idx+1, tmp, ans);\n tmp.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n string tmp;\n vector<string> ans;\n if(digits.size() == 0){\n return ans;\n }\n recur(digits, 0, tmp, ans);\n return ans;\n }\n};",
"memory": "7800"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if (digits.empty()) {\n return {};\n }\n vector<string> d = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string> ans = {\"\"};\n for (auto& i : digits) {\n string s = d[i - '2'];\n vector<string> t;\n for (auto& a : ans) {\n for (auto& b : s) {\n t.push_back(a + b);\n }\n }\n ans = move(t);\n }\n return ans;\n }\n};",
"memory": "7900"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if (digits.empty()) {\n return {};\n }\n vector<string> d = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string> ans = {\"\"};\n for (auto& i : digits) {\n string s = d[i - '2'];\n vector<string> t;\n for (auto& a : ans) {\n for (auto& b : s) {\n t.push_back(a + b);\n }\n }\n ans = move(t);\n }\n return ans;\n }\n};",
"memory": "7900"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if (digits.empty()) {\n return {};\n }\n vector<string> d = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string> ans = {\"\"};\n for (auto& i : digits) {\n string s = d[i - '2'];\n vector<string> t;\n for (auto& a : ans) {\n for (auto& b : s) {\n t.push_back(a + b);\n }\n }\n ans = move(t);\n }\n return ans;\n }\n};",
"memory": "8000"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if (digits.empty()) {\n return {};\n }\n vector<string> d = {\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string> ans = {\"\"};\n for (auto& i : digits) {\n string s = d[i - '2'];\n vector<string> t;\n for (auto& a : ans) {\n for (auto& b : s) {\n t.push_back(a + b);\n }\n }\n ans = move(t);\n }\n return ans;\n }\n};",
"memory": "8000"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n vector<string> letterCombinations(string digits) {\n if (digits.empty())\n return {};\n\n vector<string> ans;\n\n dfs(digits, 0, \"\", ans);\n return ans;\n }\n\n private:\n const vector<string> digitToLetters{\"\", \"\", \"abc\", \"def\", \"ghi\",\n \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n\n void dfs(const string& digits, int i, string&& path, vector<string>& ans) {\n if (i == digits.length()) {\n ans.push_back(path);\n return;\n }\n\n for (const char letter : digitToLetters[digits[i] - '0']) {\n path.push_back(letter);\n dfs(digits, i + 1, std::move(path), ans);\n path.pop_back();\n }\n }\n};",
"memory": "8100"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void mapping(int i, string& digits, vector<string>& dict, string comb, vector<string>& ans)\n {\n if (i==digits.size())\n {\n if (comb.size()>0)\n ans.push_back(comb);\n return;\n }\n\n for (auto d: dict[digits[i]-'0'])\n mapping(i+1, digits, dict, comb+d, ans);\n }\n vector<string> letterCombinations(string digits)\n {\n vector<string> dict = {\"\", \"\", \"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n vector<string> ans;\n string comb=\"\";\n mapping(0, digits, dict, comb, ans);\n return ans; \n }\n};",
"memory": "8100"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\n private:\n void solve(string digits,vector<string> & ans,string output,string mapping[],int idx){\n if(idx>=digits.length()){\n ans.push_back(output);\n return ;\n }\n int num=digits[idx]-'0';\n string value=mapping[num];\n\n for(int i=0;i<value.length();i++){\n output.push_back(value[i]);\n solve(digits,ans,output,mapping,idx+1);\n output.pop_back();\n }\n }\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n if(digits.length()==0){\n return ans;\n }\n string output=\"\";\n string mapping[10]={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n int idx=0;\n solve(digits,ans,output,mapping,idx);\n return ans;\n }\n};",
"memory": "8200"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\nvoid solve(vector<string> &ans,int index,string &output,string &digits,vector<string> &mapping)\n{\n if(index >= digits.length())\n {\n ans.push_back(output);\n return;\n }\n\n int digit = digits[index] - '0';\n string val = mapping[digit];\n for(int i = 0; i<val.length();i++)\n {\n char ch = val[i];\n output.push_back(ch);\n solve(ans,index+1,output,digits,mapping);\n output.pop_back();\n }\n}\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n string output = \"\";\n int index = 0;\n if(digits.length() == 0)\n {\n return ans;\n }\n\n vector<string> mapping(10);\n mapping[2] = \"abc\";\n mapping[3] = \"def\";\n mapping[4] = \"ghi\";\n mapping[5] = \"jkl\";\n mapping[6] = \"mno\";\n mapping[7] = \"pqrs\";\n mapping[8] = \"tuv\";\n mapping[9] = \"wxyz\";\n\n solve(ans,index,output,digits,mapping);\n\n return ans;\n \n }\n};",
"memory": "8200"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\n vector<string> progressive_ans_builder(vector<string> s1, string s2) {\n vector<string> s3; int n_s2 = s2.length();\n for(int i = 0; i<n_s2; i++) {\n for(int j = 0; j<s1.size(); j++) {\n string t = s1[j]+s2[i];\n s3.push_back(t);\n }\n }\n return s3;\n }\n\n vector<string> letterCombinations(string digits) {\n unordered_map<char, string> mp {\n {'2', \"abc\"}, {'3', \"def\"}, {'4', \"ghi\"},\n {'5', \"jkl\"}, {'6', \"mno\"}, {'7', \"pqrs\"},\n {'8', \"tuv\"}, {'9', \"wxyz\"}\n };\n\n if(digits.length()==0) return {};\n\n vector<string> s3 = {\"\"};\n for(int i = 0; i<digits.length(); i++) {\n string temp = mp[digits[i]];\n s3 = progressive_ans_builder(s3, temp);\n }\n\n return s3;\n\n }\n};",
"memory": "8300"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "#include <unordered_map>\n#include <vector>\n#include <string>\n\nclass Solution {\n // Mapping of digits to corresponding characters\n std::unordered_map<char, std::string> m;\n \n // Resulting combinations\n std::vector<std::string> output;\n\n // Depth-first search for generating combinations\n void dfs(const std::string& digits, int index, std::string& s) {\n // If the end of digits is reached, add the combination to the output\n if (index == digits.size()) {\n output.push_back(s);\n return;\n }\n\n // Explore all characters corresponding to the current digit\n for (char i : m[digits[index]]) {\n s.push_back(i); // Choose the character\n dfs(digits, index + 1, s); // Explore further\n s.pop_back(); // Backtrack to try other characters\n }\n }\n\npublic:\n // Constructor: Initialize the digit-to-character mapping\n Solution() : m{{'2', \"abc\"}, {'3', \"def\"}, {'4', \"ghi\"}, {'5', \"jkl\"}, {'6', \"mno\"}, {'7', \"pqrs\"}, {'8', \"tuv\"}, {'9', \"wxyz\"}} {}\n\n // Main function to generate letter combinations\n std::vector<std::string> letterCombinations(std::string digits) {\n std::string s;\n // If digits is not empty, start the depth-first search\n if (!digits.empty()) dfs(digits, 0, s);\n return output;\n }\n};\n",
"memory": "8300"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> letterCombinations(string digits) {\n if (digits.empty()) return {};\n vector<string> m = {\n \"abc\", \"def\", \"ghi\", \"jkl\",\n \"mno\", \"pqrs\", \"tuv\", \"wxyz\"\n } ;\n\n vector<string> ans = {\"\"};\n for (char digit : digits) {\n vector<string> temp;\n for (auto candidate : m[digit - '2']) {\n for (auto c : ans) {\n temp.push_back(c + candidate);\n }\n }\n ans = temp;\n }\n\n return ans;\n }\n};",
"memory": "8400"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<string> result;\n \n auto solve(int idx, string &digits, string &temp, auto &mp) {\n if(idx >= digits.length()) {\n result.push_back(temp);\n return;\n }\n \n char ch = digits[idx];\n string str = mp[ch];\n for(int i=0;i<str.length(); i++) {\n temp.push_back(str[i]);\n solve(idx+1, digits, temp, mp);\n temp.pop_back();\n \n }\n \n }\n \n vector<string> letterCombinations(string digits) {\n if(!digits.length()) return {};\n unordered_map<int,string> mp;\n mp['2'] = \"abc\";\n mp['3'] = \"def\";\n mp['4'] = \"ghi\";\n mp['5'] = \"jkl\";\n mp['6'] = \"mno\";\n mp['7'] = \"pqrs\";\n mp['8'] = \"tuv\";\n mp['9'] = \"wxyz\";\n string temp = \"\";\n \n solve(0, digits, temp, mp);\n return result;\n }\n};",
"memory": "8400"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\nvoid solve(string digit, string output, int index, vector<string>& ans, string mapping[] ) {\n \n //base case\n if(index >= digit.length()) {\n ans.push_back(output);\n return;\n }\n \n int number = digit[index] - '0';\n string value = mapping[number];\n \n for(int i=0; i<value.length(); i++) {\n output.push_back(value[i]);\n solve(digit, output, index+1, ans, mapping);\n output.pop_back();\n }\n \n }\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n if(digits.length()==0)\n return ans;\n string output;\n int index = 0;\n string mapping[10] = {\"\", \"\", \"abc\", \"def\", \"ghi\", \"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n solve(digits, output, index, ans, mapping);\n return ans;\n }\n};",
"memory": "8500"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\nprivate:\n void solve(string digit, string output, int i, vector<string>& ans, string mapping[]){\n if(i>=digit.length()){\n ans.push_back(output);\n return;\n }\n int num = digit[i] - '0';\n string value = mapping[num];\n for(int j = 0; j<value.length(); j++){\n output.push_back(value[j]);\n solve(digit, output, i+1, ans, mapping);\n output.pop_back();\n }\n }\n\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n if(digits.length() == 0){\n return ans;\n }\n int i = 0;\n string output;\n string mapping[10] = {\"\", \"\", \"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"};\n solve(digits, output, i, ans, mapping);\n return ans;\n }\n};",
"memory": "8500"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nint n;\nvector<string>result;\nvoid find(int idx,string &temp,string &digits,unordered_map<char,string>&mp){\n if(idx>=n){\n result.push_back(temp);\n return;\n }\n char ch=digits[idx];\n string str=mp[ch];\n for(int i=0;i<str.length();i++){\n temp.push_back(str[i]);\n find(idx+1,temp,digits,mp);\n temp.pop_back();\n\n }\n}\n vector<string> letterCombinations(string digits) {\n n=digits.size();\n if(n==0){\n return{};\n }\n unordered_map<char,string>mp;\n mp['2']=\"abc\";\n mp['3']=\"def\";\n mp['4']=\"ghi\";\n mp['5']=\"jkl\";\n mp['6']=\"mno\";\n mp['7']=\"pqrs\";\n mp['8']=\"tuv\";\n mp['9']=\"wxyz\";\n string temp=\"\";\n find(0,temp,digits,mp);\n return result;\n\n \n }\n};",
"memory": "8600"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\nprivate:\n void solve(string digit, string output, int index, vector<string>& ans, string mapping[] ) {\n \n //base case\n if(index >= digit.length()) {\n ans.push_back(output);\n return;\n }\n \n int number = digit[index] - '0';\n string value = mapping[number];\n \n for(int i=0; i<value.length(); i++) {\n output.push_back(value[i]);\n solve(digit, output, index+1, ans, mapping);\n output.pop_back();\n }\n \n }\npublic:\n vector<string> letterCombinations(string digits) {\n vector<string> ans;\n if(digits.length()==0)\n return ans;\n string output;\n int index = 0;\n string mapping[10] = {\"\", \"\", \"abc\", \"def\", \"ghi\", \"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n solve(digits, output, index, ans, mapping);\n return ans;\n }\n};",
"memory": "8600"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> tbl={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string> res;\n void helper(string digi, string tmp, int idx)\n {\n if(tmp.size() == digi.size())\n {\n res.push_back(tmp);\n return;\n }\n for(char i:tbl[digi[idx] - '0'])\n {\n helper(digi,tmp+i,idx+1);\n }\n }\n\n vector<string> letterCombinations(string digi) {\n if(digi.empty()) return res;\n helper(digi,\"\",0);\n return res;\n }\n};",
"memory": "8700"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<string> keys={\"\",\"\",\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"};\n vector<string> ans;\n void helper(string digits,int index,string tempans){\n if(index==digits.size()){\n if(tempans.size()!=0){\n ans.push_back(tempans);}\n return;\n }\n else {\n for(int j=0;j<keys[digits[index]-'0'].size();j++){\n // '1' ko int(1) karne se nahi hoga bhai minus '0' karo you will get extact value then x=0+y;\n \n helper(digits,index+1,tempans+keys[digits[index]-'0'][j]);\n }\n }\n }\n vector<string> letterCombinations(string digits) {\n string tempans=\"\";\n helper(digits,0,tempans);\n return ans;\n\n }\n};",
"memory": "8700"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\n int n, m;\n unordered_map<char, string> M;\n unordered_map<char, string> vis;\npublic:\n void backTrack(int idx, string s, string d, vector<string>& res) {\n if(s.size() == n) {\n res.push_back(s);\n return;\n }\n\n if(idx == n) {\n return;\n }\n\n // for(int i = 0; i < n; i++) {\n char c = d[idx];\n // if(vis[c].back() == '1')\n // continue;\n\n // vis[c].back() = '1';\n\n for(int j = 0; j < M[c].size(); j++) {\n // if(vis[c][j] == '0') {\n s += M[c][j];\n // vis[c][j] = '1';\n backTrack(idx+1, s, d, res);\n\n // vis[c][j] = '0';\n s.pop_back();\n // }\n }\n // vis[c].back() = '0';\n // }\n }\n\n vector<string> letterCombinations(string digits) {\n if(digits.size() == 0) {\n return {};\n }\n\n n = digits.size();\n vector<string> res;\n char tmp = 'a';\n for(int i = 2; i < 10; i++) {\n string s = \"\";\n int len = 3;\n if(i == 7 || i == 9) {\n len = 4;\n } \n\n for(int j = 0; j < len; j++) {\n s += tmp + j;\n }\n\n char key = '0' + i;\n M[key] = s;\n tmp = s.back() + 1;\n }\n \n for(int i = 0; i < n; i++) {\n string s = M[digits[i]];\n if(s.size() == 3) {\n vis[digits[i]] = \"0000\"; //last char is selected state of digits[i]\n } \n else\n {\n vis[digits[i]] = \"00000\";\n }\n }\n\n // for(auto it = M.begin(); it != M.end(); ++it) {\n // cout << it->first << \"-\" << it->second << endl;\n // }\n\n backTrack(0, \"\", digits, res);\n return res;\n }\n};",
"memory": "8800"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<char, string> m = {\n {'2', \"abc\"},\n {'3', \"def\"},\n {'4', \"ghi\"},\n {'5', \"jkl\"},\n {'6', \"mno\"},\n {'7', \"pqrs\"},\n {'8', \"tuv\"},\n {'9', \"wxyz\"}\n };\n vector<string> ans;\n void helper(int ind, string digits, string s){\n if(ind == digits.size()) {\n if(s!=\"\") ans.push_back(s);\n return;\n }\n\n string t = m[digits[ind]];\n for(int i = 0; i < t.size(); i++){\n s += t[i];\n helper(ind+1, digits, s);\n s.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n helper(0, digits, \"\");\n return ans;\n }\n};",
"memory": "8800"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n unordered_map<char, unordered_set<char>> dic = {\n {'2', {'a', 'b', 'c'}},\n {'3', {'d', 'e', 'f'}},\n {'4', {'g', 'h', 'i'}},\n {'5', {'j', 'k', 'l'}},\n {'6', {'m', 'n', 'o'}},\n {'7', {'p', 'q', 'r', 's'}},\n {'8', {'t', 'u', 'v'}},\n {'9', {'w', 'x', 'y', 'z'}}\n };\n vector<string> letterCombinations(string digits) {\n if (digits == \"\") {\n return {};\n }\n vector<string> res;\n dfs(digits, 0, \"\", res);\n return res;\n }\n void dfs(const string& digits, int idx, string str, vector<string> &res) {\n if (idx >= digits.size()) {\n res.push_back(str);\n return ;\n }\n for (auto ch : dic[digits[idx]]) {\n str.push_back(ch);\n dfs(digits, idx+1, str, res);\n str.pop_back();\n }\n }\n};",
"memory": "8900"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 3 | {
"code": "#define pb push_back\nclass Solution {\nprivate:\nmap<char,string>mp={\n {'2',\"abc\"},\n {'3',\"def\"},\n {'4',\"ghi\"},\n {'5',\"jkl\"},\n {'6',\"mno\"},\n {'7',\"pqrs\"},\n {'8',\"tuv\"},\n {'9',\"wxyz\"}\n };\n void solve(vector<string>&ans,string output,int i,string digits){\n if(i==digits.size()){\n if(output!=\"\")ans.pb(output);\n return;\n }\n for(int j=0;j<mp[digits[i]].length();j++){\n output+=mp[digits[i]][j];\n solve(ans,output,i+1,digits);\n output.pop_back();\n }\n }\npublic:\n vector<string> letterCombinations(string digits) {\n \n vector<string>ans;\n string output=\"\";\n solve(ans,output,0,digits);\n return ans;\n }\n};",
"memory": "8900"
} |
17 | <p>Given a string containing digits from <code>2-9</code> inclusive, return all possible letter combinations that the number could represent. Return the answer in <strong>any order</strong>.</p>
<p>A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png" style="width: 300px; height: 243px;" />
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> digits = "23"
<strong>Output:</strong> ["ad","ae","af","bd","be","bf","cd","ce","cf"]
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> digits = ""
<strong>Output:</strong> []
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> digits = "2"
<strong>Output:</strong> ["a","b","c"]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= digits.length <= 4</code></li>
<li><code>digits[i]</code> is a digit in the range <code>['2', '9']</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n void backtrack(vector<string>& res, vector<vector<char>> element, string str, int i){\n if (str.size() == element.size()){\n res.push_back(str);\n return;\n }\n\n for (int j = 0; j < element[i].size(); j++){\n str+=element[i][j];\n backtrack(res, element, str, i + 1);\n str.pop_back();\n }\n }\n vector<string> letterCombinations(string digits) {\n if (digits.size() == 0) return {};\n unordered_map<char, vector<char>> map;\n map['2'] = {'a', 'b', 'c'};\n map['3'] = {'d', 'e', 'f'};\n map['4'] = {'g', 'h', 'i'};\n map['5'] = {'j', 'k','l'};\n map['6'] = {'m', 'n', 'o'};\n map['7'] = {'p', 'q', 'r', 's'};\n map['8'] = {'t', 'u', 'v'};\n map['9'] = {'w','x','y','z'};\n\n vector<string> res;\n vector<vector<char>> element;\n for (char ch : digits){\n element.push_back(map[ch]);\n }\n\n backtrack(res, element,\"\", 0);\n\n return res;\n \n\n\n }\n};",
"memory": "9000"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.