We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents bab9c81 + 6b470e6 commit 38a5028Copy full SHA for 38a5028
03-Arrays/3sum-leetcode.cpp
@@ -0,0 +1,29 @@
1
+//Optimized Approach - O(n^2 logn + nlogn) - o(n^2 logn) time and O(n) space
2
+class Solution {
3
+public:
4
+ vector<vector<int>> threeSum(vector<int>& nums) {
5
+ int target = 0;
6
+ sort(nums.begin(), nums.end());
7
+ set<vector<int>> s;
8
+ vector<vector<int>> output;
9
+ for (int i = 0; i < nums.size(); i++){
10
+ int j = i + 1;
11
+ int k = nums.size() - 1;
12
+ while (j < k) {
13
+ int sum = nums[i] + nums[j] + nums[k];
14
+ if (sum == target) {
15
+ s.insert({nums[i], nums[j], nums[k]});
16
+ j++;
17
+ k--;
18
+ } else if (sum < target) {
19
20
+ } else {
21
22
+ }
23
24
25
+ for(auto triplets : s)
26
+ output.push_back(triplets);
27
+ return output;
28
29
+};
0 commit comments