id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n bool static cmp(string a, string b){\n return a.length()<b.length();\n }\n\n string longestCommonPrefix(vector<string>& strs) {\n sort(strs.begin(),strs.end(),cmp);\n string ans = strs[0];\n for(int i=1;i<strs.size();i++){\n int j= 0;\n string same=\"\";\n while(j<ans.length() and ans[j]==strs[i][j]){\n same+=ans[j];\n j++;\n }\n if(same==\"\"){\n return \"\";\n }\n ans=same;\n }\n return ans;\n }\n};",
"memory": "14800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string ans;\n int n=strs.size();\n int mini=INT_MAX;\n for(int i=0;i<n;i++){\n int k=strs[i].size();\n mini=min(mini,k);\n }\n for(int i=0;i<mini;i++){\n string s=strs[0].substr(0,i+1);\n int cnt=0;\n for(int j=1;j<n;j++){\n if(s==strs[j].substr(0,i+1)) cnt++;\n }\n if(cnt==n-1) ans.push_back(s[i]);\n else break;\n }\n return ans;\n }\n};",
"memory": "14800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n std::string st = strs[0], answer = \"\";\n int k = 0;\n for (int i = st.length(); i > 0; i--) {\n for (int j = 1; j < strs.size(); j++) {\n if (strs[j].find(st.substr(0, i)) == 0) {\n k++;\n }\n }\n if (k == strs.size() - 1) {\n answer = st.substr(0, i);\n return answer;\n } else {\n k = 0;\n continue;\n }\n }\n return answer;\n }\n};",
"memory": "14900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string pre=\"\";\n bool common = true;\n int size = 0;\n while(common)\n { \n // cout<<strs[0]<<\" \";\n char letter;\n if (strs[0]==\"\")\n {\n common=false;\n break;\n }\n else\n { \n letter = strs[0][0];\n strs[0] = strs[0].substr(1);\n }\n if (!common) break;\n for(int i=1;i<strs.size();i++)\n {\n // if (strs[i].length()<size || strs[i][size]!=strs[i-1][size] ||strs[i-1].length()<size )\n // {\n // common=false;\n // break;\n // }\n // cout<<strs[i]<<\" \";\n if (strs[i]==\"\" || strs[i][0]!=letter)\n {\n common=false;\n break;\n }\n else\n {\n strs[i] = strs[i].substr(1);\n }\n }\n // cout<<\"\\n\";\n if (!common) break;\n \n pre+=letter;\n // size++;\n }\n return pre;\n }\n};",
"memory": "15000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string str = \"\";\n\n for (int i = 0; i < strs[0].length(); i++) {\n\n str += strs[0][i];\n for (int j = 1; j < strs.size(); j++) {\n\n if (strs[j].substr(0,i+1).find(str) == string::npos) {\n str.pop_back();\n return str;\n }\n }\n }\n return str;\n }\n};",
"memory": "15000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string prefix = \"\";\n\n int mi = strs[0].length();\n\n for(int i=1;i<strs.size();i++){\n mi = min(mi, (int)strs[i].length());\n }\n \n\n for(int i=0;i<mi;i++){\n prefix = prefix + strs[0][i];\n bool flag = true;\n for(int j=0;j<strs.size();j++){\n if(prefix != strs[j].substr(0,i+1)){\n flag = false;\n break;\n }\n }\n if(!flag){\n prefix.pop_back();\n }\n }\n\n\n return prefix;\n }\n};",
"memory": "15100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string re = \"\";\n int len = 1;\n bool isEnd=false;\n for (int len = 1; len <= strs.at(0).size(); len++) {\n for (int i = 1; i < strs.size(); i++) {\n int pos=strs.at(i).find(strs.at(0).substr(0, len));\n if (0!=pos) {\n isEnd=true;\n break;\n }\n }\n if(!isEnd){\n re=strs.at(0).substr(0, len);\n }\n else{\n break;\n }\n }\n return re;\n }\n};",
"memory": "15100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string pre=\"\";\n bool common = true;\n int size = 0;\n while(common)\n { \n // cout<<strs[0]<<\" \";\n char letter;\n if (strs[0]==\"\")\n {\n common=false;\n break;\n }\n else\n { \n letter = strs[0][0];\n strs[0] = strs[0].substr(1);\n }\n if (!common) break;\n for(int i=1;i<strs.size();i++)\n {\n // if (strs[i].length()<size || strs[i][size]!=strs[i-1][size] ||strs[i-1].length()<size )\n // {\n // common=false;\n // break;\n // }\n // cout<<strs[i]<<\" \";\n if (strs[i]==\"\" || strs[i][0]!=letter)\n {\n common=false;\n break;\n }\n else\n {\n strs[i] = strs[i].substr(1);\n }\n }\n // cout<<\"\\n\";\n if (!common) break;\n \n pre+=letter;\n // size++;\n }\n return pre;\n }\n};",
"memory": "15200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string pre=\"\";\n bool common = true;\n int size = 0;\n while(common)\n { \n // cout<<strs[0]<<\" \";\n char letter;\n if (strs[0]==\"\")\n {\n common=false;\n break;\n }\n else\n { \n letter = strs[0][0];\n strs[0] = strs[0].substr(1);\n }\n if (!common) break;\n for(int i=1;i<strs.size();i++)\n {\n // if (strs[i].length()<size || strs[i][size]!=strs[i-1][size] ||strs[i-1].length()<size )\n // {\n // common=false;\n // break;\n // }\n // cout<<strs[i]<<\" \";\n if (strs[i]==\"\" || strs[i][0]!=letter)\n {\n common=false;\n break;\n }\n else\n {\n strs[i] = strs[i].substr(1);\n }\n }\n // cout<<\"\\n\";\n if (!common) break;\n \n pre+=letter;\n // size++;\n }\n return pre;\n }\n};",
"memory": "15200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string dum=\"\";\n vector<string>v;\n int min=strs[0].size();\n for(int i=0;i<strs.size();i++){\n if(min>strs[i].size())\n min=strs[i].size();\n }\n for(int i=0;i<min;i++){\n // for(int j=i;j<min;j++){\n\n int cnt=0;\n string dum=strs[0].substr(0,i+1);\n for(int k=0;k<strs.size();k++){\n if(dum==strs[k].substr(0,i+1)){\n cnt++;\n \n }\n }\n if(cnt==strs.size())\n v.push_back(dum);\n \n \n // }\n }\n \n int max=1;\n for(int i=0;i<v.size();i++){\n if(max<v[i].size())\n max=v[i].size();\n }\n string ans=\"\";\n for(int i=0;i<v.size();i++){\n if(max==v[i].size()){\n ans=v[i];\n break;\n }\n }\n for(int i=0;i<v.size();i++)\n cout<<v[i]<<\"\";\n return ans;\n }\n};",
"memory": "15300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n for (const auto &str : strs) {\n if (str == \"\") {\n return \"\";\n }\n }\n char firstChar = strs[0][0];\n for (const auto &str : strs) {\n if (str[0] != firstChar) {\n return \"\";\n }\n }\n for (auto &str : strs) {\n str = str.substr(1);\n }\n return firstChar + longestCommonPrefix(strs);\n }\n};",
"memory": "15400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n return lcp(strs, 0, strs.size()-1);\n }\n string CommonPrefixUtil(string str1, string str2){\n string result;\n int n1 = str1.length(), n2 = str2.length();\n\n for(int i=0,j=0; i<=n1-1 && j <= n2-1; i++,j++){\n if(str1[i] != str2[j]){\n break;\n }\n result.push_back(str1[i]);\n }\n return result;\n }\n\n string lcp(vector<string>& s, int low, int high){\n if(low == high){\n return s[low];\n }\n if(high > low){\n int mid = low + (high - low)/2;\n string str1 = lcp(s, low, mid);\n string str2 = lcp(s, mid+1, high);\n\n return CommonPrefixUtil(str1, str2);\n }\n else{\n return s[low];\n }\n }\n};",
"memory": "15500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string CommonPrefix(string strs1, string strs2) {\n string ans = \"\";\n int i;\n for (i = 0; i < min(strs1.size(),strs2.size()); i++) {\n if (strs1[i] != strs2[i]) {\n break;\n }\n }\n ans = strs1.substr(0, i );\n return ans;\n }\n\n string LongestPrefix(vector<string>& strs, int start, int end) {\n if(start==end){\n return strs[start];\n }\n int mid = (start + end) / 2;\n\n string strs1 = LongestPrefix(strs, start, mid);\n string strs2 = LongestPrefix(strs, mid + 1, end);\n\n return CommonPrefix(strs1, strs2);\n }\n\n string longestCommonPrefix(vector<string>& strs) {\n std::ios_base::sync_with_stdio(false);\n\n std::cout.tie(nullptr);\n\n std::cin.tie(nullptr);\n\n return LongestPrefix(strs, 0, strs.size()-1);\n }\n};",
"memory": "15600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n string merge(string s1, string s2){\n if(s1==\"\") return \"\";\n if(s2==\"\") return \"\";\n string s = \"\";\n for(int i = 0; i<min(s1.size(), s2.size()); i++){\n if(s1[i]==s2[i]) s+=s1[i];\n else break;\n }\n return s;\n }\n string lcp(vector<string>& strs, int i, int j){\n if(i>j) return \"\";\n if(i==j) return strs[i];\n int mid = i + (j-i)/2;\n string s1 = lcp(strs, i, mid);\n string s2 = lcp(strs, mid+1, j);\n return merge(s1, s2);\n }\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n return lcp(strs, 0, strs.size()-1);\n\n \n }\n};",
"memory": "15700"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string common(string a, string b) {\n string ans = \"\";\n int i = 0;\n while(i < a.size() && i< b.size()) {\n if (a[i] != b[i])break;\n ans += a[i];\n i++;\n }\n return ans;\n }\n\n string calculate(int start, int end, vector<string>& strs) {\n // cout <<\"start=\"<<start<<\" end=\"<<end<<endl;\n int diff = (end-start)/2;\n if (end - start == 1) return strs[start];\n string ans1 = calculate(start,start+diff,strs);\n string ans2 = calculate(start+diff,end,strs);\n string ans = common(ans1,ans2);\n return ans;\n }\n\n string longestCommonPrefix(vector<string>& strs) {\n string ans = calculate(0, strs.size(), strs);\n return ans;\n }\n};",
"memory": "15800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <unordered_map>\n#include <memory>\n\nstruct Trie\n{\n string value;\n unordered_map<string, std::shared_ptr<Trie>> children;\n Trie(string value) : value(value) { }\n\n std::shared_ptr<Trie> insert (string value)\n {\n auto node = children.find(value);\n if (node != children.end())\n return node->second;\n\n auto ref = std::make_shared<Trie>(Trie(value));\n children.insert({ value, ref });\n return children[value];\n }\n\n void insert (string& s, int offset, int length=-1)\n {\n if (length == -1)\n length = s.size();\n\n if (offset >= length) {\n insert(\"\");\n return;\n }\n\n insert(s.substr(offset, 1))->insert(s, offset+1, length);\n }\n\n string getString() {\n if (children.size() == 1)\n return value + children.begin()->second->getString();\n return value;\n }\n};\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs)\n {\n Trie root(\"\");\n for (string& value : strs)\n root.insert(value, 0);\n return root.getString();\n }\n};",
"memory": "16000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n /* \n //This finds the common series of alphabets between strings. But the question wants only prefix. so we need to remove first loop.\n string common = \"\"; \n string common_guess = \"\";\n int n_strings = strs.size();\n int min_len = 100;\n int counter = 0;\n \n for(auto a: strs){\n if (a.length() < min_len){\n min_len = a.length();\n common_guess = a; \n }\n }\n \n common = common_guess;\n\n for(int i = 0; i < min_len -1; i++){\n for(int j = min_len; j > i; j --){\n common = common_guess.substr(i, j);\n counter = 0;\n for(auto a: strs){\n if (a.find_first_of(common_guess)==-1)\n return \"\";\n if (a.find(common) == -1)\n break;\n counter ++;\n if (counter == n_strings)\n return common;\n }\n }\n }*/\n \n //The following code is for prefix.\n \n string common = \"\"; \n string common_guess = \"\";\n int n_strings = strs.size();\n int max_len = 0;\n int counter = 0;\n \n for(auto a: strs){\n if (a.length() > max_len){\n max_len = a.length();\n common_guess = a; \n }\n }\n \n common = common_guess;\n \n for(int j = max_len; j > 0; j --){\n common = common_guess.substr(0, j);\n counter = 0;\n for(auto a: strs){\n if (a.find_first_of(common_guess)==-1)\n return \"\";\n if (a.substr(0, j).find(common, 0) == -1)\n break;\n counter ++;\n if (counter == n_strings)\n return common;\n }\n }\n \n return \"\";\n }\n};",
"memory": "16100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n static bool comp(string & v1, string & v2){\n return v1.size() > v2.size();\n }\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n\n int s_size = strs.size();\n if (s_size == 0) return \"\";\n else if (s_size == 1) return strs[0];\n\n sort(strs.begin(), strs.end(), comp);\n\n list<string> subs;\n\n for(int l = strs[0].length(); l > 0; --l){\n string sub = strs[0].substr(0, l);\n bool all_suff = true;\n for (int j = 1; j < s_size; ++j)\n if (sub != strs[j].substr(0, l)){\n all_suff = false;\n break; \n }\n if (all_suff) return sub;\n } \n\n return \"\";\n }\n\n \n};",
"memory": "16200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n = strs.size();\n\n string minLenStr = strs[ 0 ];\n for( int i = 1; i < n; i++ ) {\n if( strs[ i ].size() < minLenStr.size() ) {\n minLenStr = strs[ i ];\n }\n }\n\n // Now create a hashMap of all the windows of the word with minimum length\n unordered_map< string, int > numWordsStartingWith;\n for( int i = 0; i < minLenStr.size(); i++ ) {\n numWordsStartingWith.insert( { minLenStr.substr( 0, i+1 ), 0} );\n }\n\n for( const auto& str : strs ) {\n for( int i = 0; i < minLenStr.size(); i++ ) {\n string prefix = str.substr( 0, i+1 );\n if( numWordsStartingWith.find( prefix ) == numWordsStartingWith.end() ) {\n continue;\n }\n numWordsStartingWith[ prefix ]++;\n }\n }\n\n string result;\n for( const auto& [ prefix, numWords ] : numWordsStartingWith ) {\n if( numWords == n ) {\n result = ( prefix.size() > result.size() ) ? prefix : result;\n }\n }\n\n return result;\n }\n};",
"memory": "16300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n = strs.size();\n\n string minLenStr = strs[ 0 ];\n for( int i = 1; i < n; i++ ) {\n if( strs[ i ].size() < minLenStr.size() ) {\n minLenStr = strs[ i ];\n }\n }\n\n // Now create a hashMap of all the windows of the word with minimum length\n unordered_map< string, int > numWordsStartingWith;\n for( int i = 0; i < minLenStr.size(); i++ ) {\n numWordsStartingWith.insert( { minLenStr.substr( 0, i+1 ), 0} );\n }\n\n for( const auto& str : strs ) {\n for( int i = 0; i < minLenStr.size(); i++ ) {\n string prefix = str.substr( 0, i+1 );\n if( numWordsStartingWith.find( prefix ) == numWordsStartingWith.end() ) {\n continue;\n }\n numWordsStartingWith[ prefix ]++;\n }\n }\n\n string result;\n for( const auto& [ prefix, numWords ] : numWordsStartingWith ) {\n if( numWords == n ) {\n result = ( prefix.size() > result.size() ) ? prefix : result;\n }\n }\n\n return result;\n }\n};",
"memory": "16400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string prefix=strs[0];\n string aux=\"\";\n for(int i=1;i<strs.size();i++){\n for(int j=0;j<prefix.length();j++){\n if(j<strs[i].length()){\n if(prefix[j]==strs[i][j]){\n aux=aux+prefix[j];\n }\n else{\n break;\n }\n }\n }\n prefix=aux;\n if(aux.empty()==true){\n return aux;\n }\n aux.clear();\n }\n return prefix;\n }\n};",
"memory": "16900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size() == 1)\n return strs[0];\n\n string ans = strs[0];\n\n for (int i = 1; i < strs.size(); i++){\n string temp = \"\";\n\n for (int j = 0; j < min(strs[i].size(), ans.size()); j++){\n if (ans[j] == strs[i][j])\n temp = temp + ans[j];\n else \n break;\n }\n\n ans = temp;\n }\n\n return ans;\n }\n // Time complexity - O(n)\n // where, n - sum of all characters strs\n\n // Space complexity - O(n)\n // where, n - length of the longest common prefix\n};",
"memory": "17000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string commonPrefix = strs[0];\n for(int i=1;i<strs.size();i++){\n string data = strs[i];\n string finalPrefix;\n for(int j=0;j<data.size();j++){\n if(data[j]==commonPrefix[j]){\n finalPrefix = finalPrefix + data[j];\n }\n else{\n break;\n }\n }\n commonPrefix = finalPrefix;\n } \n return commonPrefix;\n }\n};",
"memory": "17100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n = strs.size();\n int ans = 0;\n string prefix = \"\";\n for(int length = 1; length < 200; length++) {\n bool flag = true;\n string substr = \"\";\n for(int i=0; i<n;i++) {\n if (strs[i].size() < length) {\n flag = false;break;\n }\n if (substr == \"\") {\n substr = strs[i].substr(0, length);\n } else {\n if(substr != strs[i].substr(0, length)) {\n flag = false;\n break;\n }\n }\n }\n \n if (flag) {\n ans = length;\n prefix = substr;\n cout << substr << \"\\n\";\n }\n }\n return prefix;\n }\n};",
"memory": "17200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string longest_CP = strs[0];\n for(auto s:strs){\n int i=0;\n string ans = \"\";\n while(i<s.size() && i<longest_CP.size() && longest_CP[i] == s[i]){\n ans = ans + s[i];\n i++;\n }\n\n longest_CP = ans;\n }\n return longest_CP;\n }\n};",
"memory": "17300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longest(string& str,string& str1){\n int i=0;\n int j = 0;\n string s = \"\";\n\n while(i<str.length() && j<str1.length()){\n if(str[i]==str1[j]){\n s = s + str[i];\n i++;\n j++;\n }\n else{\n break;\n }\n }\n\n return s;\n }\n string longestCommonPrefix(vector<string>& strs) {\n\n if(strs.size()==1){\n return strs[0];\n }\n string str = strs[0];\n string str2 = \"\";\n\n for(int i=1;i<strs.size();i++){\n\n string str1 = strs[i];\n\n str2 = longest(str,str1);\n\n str = str2;\n }\n\n return str2;\n }\n};",
"memory": "17400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size()==1)\n {\n return strs[0];\n }\n string s=\"\";\n string s1=strs[0];\n string s2=strs[1];\n int min_length=min(s1.length(),s2.length());\n for(int i=0;i<min_length;i++)\n {\n if(s1[i]==s2[i])\n {\n s=s+s1[i];\n }\n else{\n break;\n }\n }\n if(strs.size()==2)\n {\n return s;\n }\n string s3=s;cout<<s;\n for(int i=2;i<strs.size();i++)\n {\n string s4=strs[i];\n min_length=min(s3.length(),s4.length());\n string s5=\"\";\n // cout<<min_length;\n for(int j=0;j<min_length;j++)\n {\n if(s3[j]==s4[j])\n {\n s5=s5+s3[j];\n \n }\n else{\n break;\n }\n }\n s3=s5;\n\n }\n return s3;\n\n }\n};",
"memory": "17500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string findPrefixTwo(string a, string b){\n //using two pointer each on a and b\n int i =0; \n int j = 0;\n\n string ans = \"\";\n\n while(i<a.size() || j<b.size()){\n if(a[i] == b[j]){\n ans = ans+a[i];\n i++, j++;\n }\n\n else{\n return ans;\n }\n }\n\n return ans;\n}\n\n string longestCommonPrefix(vector<string>& arr) {\n if(arr.size() == 1){\n return arr[0];\n }\n\n //compare first two strings\n string prefix2 = findPrefixTwo(arr[0], arr[1]);\n\n if(arr.size()==2){\n return prefix2;\n }\n\n //now compare prefixes for third element to last element and update it\n string ans = \"\";\n for(int i =2; i<arr.size(); i++){\n ans = findPrefixTwo(prefix2, arr[i]);\n\n prefix2 = ans;\n }\n return ans;\n }\n};",
"memory": "17600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string prefixx(string s1, string s2){\n string ans = \"\";\n int i = 0 ;\n int n1 = s1.size();\n int n2 = s2.size();\n\n //if(n1 == 0 || n2 == 0 ) return ans;\n\n while(i< min(n1,n2)){\n if(s1[i] == s2[i]){\n ans = ans+s1[i];\n }\n else return ans ;\n i++;\n }\n return ans ;\n }\n string longestCommonPrefix(vector<string>& strs) {\n\n string prefix = strs[0];\n\n for(int i = 1 ; i<strs.size(); i++){\n prefix = prefixx(prefix,strs[i]);\n }\n return prefix;\n }\n};",
"memory": "17600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string line1 = strs[0];\n int result = 1;\n int counter = 0;\n if(strs.size() == 1)\n return strs[0];\n for(int i = 1; i <= line1.size(); i++)\n {\n for(int j = 1; j < strs.size(); j++)\n {\n if(line1.substr(0, i) != strs[j].substr(0,i))\n {\n result = 0;\n break;\n }\n }\n if(result == 0)\n {\n return line1.substr(0,i - 1);\n }\n counter++;\n }\n return line1.substr(0,counter);\n }\n};",
"memory": "17700"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int k = strs[0].size();\n string current = \"\";\n string after = \"\";\n bool whoops = false;\n string ans = \"\";\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n ans = current;\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n return ans;\n }\n};",
"memory": "17800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string compareTwoWords(string maxPreNow, string word){\n string newPrefix = \"\";\n for(int i = 0; i < word.size(); i++){\n if(word[i] == maxPreNow[i]){\n newPrefix = newPrefix + word[i];\n } else break;\n }\n return newPrefix;\n }\n\n string longestCommonPrefix(vector<string>& strs) {\n string maxPreNow = strs[0];\n for(string word : strs){\n maxPreNow = compareTwoWords(maxPreNow, word);\n }\n return maxPreNow;\n }\n};",
"memory": "17800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n unordered_set<char> set;\n int k = strs[0].size();\n string current = \"\";\n string after = \"\";\n bool whoops = false;\n vector<string> vect;\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n vect.push_back(current);\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n /*for(int i = 0; i < vect.size(); i ++){\n cout << vect[i] << endl;\n }*/\n\n if(vect.size() <= 0){\n return \"\";\n }\n\n return vect[vect.size() - 1];\n }\n};",
"memory": "17900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n unordered_set<char> set;\n int k = strs[0].size();\n string current = \"\";\n string after = \"\";\n bool whoops = false;\n vector<string> vect;\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n vect.push_back(current);\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n if(vect.size() <= 0){\n return \"\";\n }\n\n return vect[vect.size() - 1];\n }\n};",
"memory": "17900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n unordered_set<char> set;\n int k = strs[0].size();\n string current = \"\";\n string after = \"\";\n bool whoops = false;\n vector<string> vect;\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n vect.push_back(current);\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n if(vect.size() <= 0){\n return \"\";\n }\n\n return vect[vect.size() - 1];\n }\n};",
"memory": "18000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int k = strs[0].size();\n string current;\n string after;\n bool whoops = false;\n string ans = \"\";\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n ans = current;\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n return ans;\n }\n};",
"memory": "18000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int k = strs[0].size();\n string current;\n string after;\n bool whoops = false;\n string ans = \"\";\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n ans = current;\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n return ans;\n }\n};",
"memory": "18100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int r ;\n for ( r = 0 ; ; r ++ ){\n bool possible = true;\n if ( strs[0].size() <= r) break;\n char c = strs[0][r];\n for ( string s: strs) {\n if (r >= s.size()) {\n possible = false;\n break;\n }\n if (s[r] != c) {\n possible = false;\n break;\n }\n }\n if (!possible) break;\n }\n return strs[0].substr(0, r);\n \n }\n};",
"memory": "18100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n unordered_set<char> set;\n int k = strs[0].size();\n string current = \"\";\n string after = \"\";\n bool whoops = false;\n vector<string> vect;\n string ans = \"\";\n\n if(strs.size() ==1){\n return strs[0];\n }\n\n for(int i = 0; i < strs.size(); i++){\n k = min(k, (int)strs[i].size());\n }\n \n for(int i = 0; i < k; i++){\n for(int j = 0; j < strs.size() - 1; j++){\n current = strs[j].substr(0,i+1);\n after = strs[j + 1].substr(0,i+1);\n if(current != after){\n whoops = true;\n break;\n }\n if(current == after && j == strs.size() - 2){\n vect.push_back(current);\n ans = current;\n }\n }\n if(whoops){\n break;\n }\n\n }\n\n if(vect.size() <= 0){\n return \"\";\n }\n\n //return vect[vect.size() - 1];\n return ans;\n }\n};",
"memory": "18200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string ans;\n int i = 0;\n while(true){\n char curr_ch = 0;\n for(auto str: strs){\n if(i >= str.size()){\n curr_ch = 0;\n break;\n }\n if(curr_ch == 0){\n curr_ch =str[i];\n }\n else if(str[i] != curr_ch){\n curr_ch = 0;\n break;\n }\n }\n if(curr_ch == 0){\n break;\n }\n ans.push_back(curr_ch);\n i++;\n }\n return ans;\n }\n};",
"memory": "18200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n \n string ans;\n int i=0;\n while(true){\n char curr_ch = 0;\n for(auto str: strs){\n if(i>= str.size()) {\n curr_ch = 0;\n break;\n }\n\n if(curr_ch == 0){\n curr_ch = str[i];\n }\n\n else if(str[i] != curr_ch){\n curr_ch = 0;\n break;\n }\n }\n\n if(curr_ch == 0){\n break;\n }\n\n ans.push_back(curr_ch);\n i++;\n }\n\n return ans;\n }\n};",
"memory": "18300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string res = \"\";\n\n for(int i = 0;i<strs[0].size();i++)\n {\n for(auto s:strs)\n {\n if (s[i]!= strs[0][i])\n {\n return res;\n }\n }\n res += strs[0][i];\n }\n return res;\n }\n};",
"memory": "18300"
} |
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>
| 0 | {
"code": "#include <execution>\n\nconst size_t POOL_SIZE = std::pow(1024, 3) * 0.1; // 1 GB\nvoid* gPool = std::malloc(POOL_SIZE);\nvoid* gPoolPtr = gPool;\n\nvoid* operator new(size_t size) {\n // Определяем выравнивание\n const size_t alignment = alignof(std::max_align_t);\n\n // Определяем доступное количество памяти\n size_t space = (char*)gPool + POOL_SIZE - (char*)gPoolPtr;\n\n // Выравниваем указатель\n void* p = gPoolPtr;\n if (std::align(alignment, size, p, space)) {\n gPoolPtr = (char*)p + size;\n //std::cout << \"Custom new: Allocating \" << size << \" bytes\" << std::endl;\n return p;\n }\n return 0;\n}\n\nvoid operator delete(void* p) noexcept {\n // Реально память пула не освобождается, т.к. это пул статического размера\n}\n\n\nclass Solution {\npublic:\n\n\n\nstd::vector<std::vector<int>> threeSum(std::vector<int>& nums) {\n using value_t = typename std::remove_reference_t<decltype(nums)>::value_type;\nstd::vector<std::vector<value_t>> result;\nconst auto size = nums.size();\nif (size < 3) {\n return result;\n}\n\nstd::sort(nums.data(), nums.data() + size);\nauto tmp = nums.data();\n{\n auto nums = tmp;\n for (size_t i = 0; i < size - 2; i++) {\n if (i != 0 && nums[i - 1] == nums[i]) {\n continue;\n }\n size_t l = i + 1;\n size_t r = size - 1;\n while (l < r) {\n value_t sum = nums[i] + nums[l] + nums[r];\n if (sum == 0) {\n result.emplace_back(std::vector{ nums[i], nums[l], nums[r] });\n l++;\n r--;\n while (l < r && nums[l] == nums[l - 1]) l++;\n while (l < r && nums[r] == nums[r + 1]) r--;\n }\n else if (sum > 0) {\n r--;\n }\n else {\n l++;\n }\n }\n }\n}\nreturn result;\n}\n\n\n\n\n};",
"memory": "27255"
} |
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>
| 0 | {
"code": "class Solution {\npublic:\n void bubbleSort(vector<int>& nums) {\n int n = nums.size();\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - i - 1; j++) {\n if (nums[j] > nums[j + 1]) {\n swap(nums[j], nums[j + 1]);\n }\n }\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> result;\n bubbleSort(nums);\n int n = nums.size();\n for (int i = 0; i < n - 2; i++) {\n if (i > 0 && nums[i] == nums[i - 1]) continue;\n int left = i + 1;\n int right = n - 1;\n while (left < right) {\n int sum = nums[i] + nums[left] + nums[right];\n if (sum == 0) {\n result.push_back({nums[i], nums[left], nums[right]});\n while (left < right && nums[left] == nums[left + 1]) left++;\n while (left < right && nums[right] == nums[right - 1]) right--;\n left++;\n right--;\n } else if (sum < 0) {\n left++;\n } else {\n right--;\n }\n }\n } \n return result;\n }\n};\n",
"memory": "27255"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n\n vector<pair<int, int>> new_pos{{0, 1}};\n new_pos.reserve(nums.size());\n for (int i = 1; i < nums.size(); i++) {\n if (nums[i] == nums[i - 1]) {\n new_pos.back().second++;\n } else {\n new_pos.emplace_back(i, 1);\n }\n }\n\n unordered_set<int> nums_set(nums.begin(), nums.end());\n vector<vector<int>> result;\n int target = 0;\n for (int a = 0; a < new_pos.size(); a++) {\n const auto &[i, cnta] = new_pos[a];\n const int numsi = nums[i];\n if (numsi > 0) {\n break;\n }\n if (numsi == 0) {\n if (cnta >= 3) {\n result.push_back({0, 0, 0});\n }\n break;\n }\n target = -2 * numsi;\n if (cnta >= 2 && nums_set.contains(target)) {\n result.push_back({numsi, numsi, target});\n }\n for (int b = a + 1; b < new_pos.size(); b++) {\n const auto &[j, cntb] = new_pos[b];\n const int numsj = nums[j];\n if (numsj > -numsi / 2) {\n break;\n }\n if (numsi + 2 * numsj == 0) {\n if (cntb >= 2) {\n result.push_back({numsi, numsj, numsj});\n }\n break;\n }\n target = -numsi - numsj;\n if (nums_set.contains(target)) {\n result.push_back({numsi, numsj, target});\n }\n }\n }\n\n return result;\n }\n};",
"memory": "32166"
} |
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>
| 2 | {
"code": "class Solution {\npublic:\n vector<pair<int, int>> helper(vector<int>& nums, int start, int end, int target) {\n vector<pair<int, int>> pairs;\n while (start < end) {\n int sum = nums[start] + nums[end];\n if (sum < target) {\n start++;\n } else if (sum == target) {\n pairs.push_back({nums[start], nums[end]});\n start++;\n end--;\n // Skip duplicates for the second and third elements\n while (start < end && nums[start] == nums[start - 1]) start++;\n while (start < end && nums[end] == nums[end + 1]) end--;\n } else {\n end--;\n }\n }\n return pairs;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> ans;\n sort(nums.begin(), nums.end());\n int n = nums.size();\n \n for (int i = 0; i < n - 2; i++) {\n if (i > 0 && nums[i] == nums[i - 1]) continue;\n\n int sumToFind = -nums[i];\n auto pairs = helper(nums, i + 1, n - 1, sumToFind);\n for (auto p : pairs) {\n ans.push_back({nums[i], p.first, p.second});\n }\n }\n\n return ans;\n }\n};\n",
"memory": "32166"
} |
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\n{\npublic:\n vector<vector<int>> threeSum(vector<int> &nums)\n {\n\n set<vector<int>> s;\n sort(nums.begin(), nums.end());\n vector<int> v;\n\n vector<vector<int>> vv;\n int size = nums.size();\n\n if (size < 3)\n return vv;\n\n // can't write here like if(nums[0]>0 && size<3) return vv;\n // cause if size is zero(empty vector) then here you are checking on nums[0] >0... \n // out of bound memory runtime error\n\n\n if (nums[0] > 0)\n return vv; \n //if first element in sorted array is greater than 0 \n //then no way triplet can be 0\n \n\n int j, k;\n for (int i = 0; i < size - 2; i++)\n {\n j = i + 1, k = size - 1;\n while (j < k)\n {\n if (nums[j] + nums[k] == -nums[i])\n {\n v.push_back(nums[i]);\n v.push_back(nums[j]);\n v.push_back(nums[k]);\n s.insert(v);\n v.clear();\n }\n if (nums[j] + nums[k] > -nums[i])\n k--;\n else\n j++;\n }\n }\n for (auto i = s.begin(); i != s.end(); i++)\n {\n vv.push_back(*i);\n }\n return vv;\n }\n};",
"memory": "37078"
} |
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()-1;\n vector<vector<int>> res;\n set<vector<int>> s;\n sort(nums.begin(),nums.end());\n for (int i=0; i<n-1; i++){\n if (i>0 && nums[i]==nums[i-1]) continue;\n int j = i+1;\n int k = n;\n // cout << i << nums[i];\n while (j<k){\n if (nums[j]+nums[k]==(0-nums[i])){\n s.insert({nums[i],nums[j],nums[k]});\n k--;\n j++;\n while (j < k && nums[j] == nums[j - 1])\n j++;\n while (j < k && nums[k] == nums[k + 1])\n k--;\n }\n else if (nums[j]+nums[k]>(0-nums[i])){\n k--;\n }\n else j++;\n }\n\n }\n for (auto& ch : s){\n res.push_back(ch);\n }\n return res;\n }\n};",
"memory": "37078"
} |
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>> s1;\n // for(int i=0;i<nums.size();i++)\n // {\n // for(int j=i+1;j<nums.size();j++)\n // {\n // for(int k=j+1;k<nums.size();k++)\n // {\n // if(nums[i]+nums[j]+nums[k]==0)\n // {\n // vector<int> temp={nums[i],nums[j],nums[k]};\n // sort(temp.begin(),temp.end());\n // s1.insert(temp);\n // }\n // }\n // }\n // }\n // for(auto it:s1)\n // {\n // ans.push_back(it);\n // }\n // return ans;\n// vector< vector<int> >ans;\n// set<vector<int>> temp;\n// for(int i=0;i<nums.size();i++){\n// unordered_map<int,int> umap;\n// for(int j=i+1;j<nums.size();j++)\n// {\n// int t=-(nums[i]+nums[j]);\n// if(umap.find(t)!=umap.end()){\n// vector<int> temp2={nums[i],nums[j],t};\n// sort(temp2.begin(),temp2.end());\n// temp.insert(temp2);\n// }\n// umap.insert({nums[j],j});\n\n// }\n// }\n\n// for(auto it: temp){\n// ans.push_back(it);\n// }\n// return ans;\n vector<vector<int>> ans;\n set<vector<int>> s1;\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size();i++)\n {\n if(i>0&&nums[i]==nums[i-1])\n {\n continue;\n }\n int s=i+1;\n int e=nums.size()-1;\n while(s<e)\n {\n int sum=nums[i]+nums[s]+nums[e];\n if(sum<0)\n {\n s++; \n }\n else if(sum>0)\n {\n e--;\n }\n else{\n vector<int> temp={nums[i],nums[s],nums[e]};\n s1.insert(temp);\n s++;\n e--;\n // while(s<e&&nums[s]!=nums[s-1]){\n // s++;\n // }\n // while(s<e&&nums[e]!=nums[e+1]){\n // e--;\n // }\n \n }\n\n }\n }\n for(auto it:s1 )\n {\n ans.push_back(it);\n }\n return ans;\n }\n\n};",
"memory": "41989"
} |
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>> s1;\n // for(int i=0;i<nums.size();i++)\n // {\n // for(int j=i+1;j<nums.size();j++)\n // {\n // for(int k=j+1;k<nums.size();k++)\n // {\n // if(nums[i]+nums[j]+nums[k]==0)\n // {\n // vector<int> temp={nums[i],nums[j],nums[k]};\n // sort(temp.begin(),temp.end());\n // s1.insert(temp);\n // }\n // }\n // }\n // }\n // for(auto it:s1)\n // {\n // ans.push_back(it);\n // }\n // return ans;\n// vector< vector<int> >ans;\n// set<vector<int>> temp;\n// for(int i=0;i<nums.size();i++){\n// unordered_map<int,int> umap;\n// for(int j=i+1;j<nums.size();j++)\n// {\n// int t=-(nums[i]+nums[j]);\n// if(umap.find(t)!=umap.end()){\n// vector<int> temp2={nums[i],nums[j],t};\n// sort(temp2.begin(),temp2.end());\n// temp.insert(temp2);\n// }\n// umap.insert({nums[j],j});\n\n// }\n// }\n\n// for(auto it: temp){\n// ans.push_back(it);\n// }\n// return ans;\n vector<vector<int>> ans;\n set<vector<int>> s1;\n sort(nums.begin(),nums.end());\n for(int i=0;i<nums.size();i++)\n {\n if(i>0&&nums[i]==nums[i-1])\n {\n continue;\n }\n int s=i+1;\n int e=nums.size()-1;\n while(s<e)\n {\n int sum=nums[i]+nums[s]+nums[e];\n if(sum<0)\n {\n s++; \n }\n else if(sum>0)\n {\n e--;\n }\n else{\n vector<int> temp={nums[i],nums[s],nums[e]};\n s1.insert(temp);\n s++;\n e--;\n // while(s<e&&nums[s]!=nums[s-1]){\n // s++;\n // }\n // while(s<e&&nums[e]!=nums[e+1]){\n // e--;\n // }\n \n }\n\n }\n }\n for(auto it:s1 )\n {\n ans.push_back(it);\n }\n return ans;\n }\n\n};",
"memory": "41989"
} |
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>> twosum(vector<int>& nums, int start, int target){\n int tmp, l=start, r=nums.size()-1;\n vector<vector<int>> ans;\n while(l<r){\n tmp=nums[l]+nums[r];\n if(tmp>target)\n r--;\n else if(tmp==target){\n ans.push_back({nums[r], nums[l]});\n l++;\n r--;\n }\n else\n l++;\n }\n return ans;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int target;\n set<vector<int>> all;\n for (int i=0; i<nums.size()-2; i++){\n if(i!=0 && nums[i]==nums[i-1])\n continue;\n target=-nums[i];\n vector<vector<int>> tmp=twosum(nums,i+1,target);\n for(int j=0; j<tmp.size(); j++){\n tmp[j].push_back(nums[i]);\n all.insert(tmp[j]);\n }\n }\n vector<vector<int>> ans(all.begin(),all.end());\n return ans;\n }\n};",
"memory": "46900"
} |
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 print(unordered_map<int, int> mp){\n for(pair<int, int> p : mp){\n cout << p.first << \" \" << p.second << endl;\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n int n = nums.size();\n vector<vector<int> > ans;\n sort(nums.begin(), nums.end());\n unordered_map<int, int> counts;\n for(int i : nums){\n counts[i]++;\n }\n vector<int> unique;\n for(pair<int, int> p : counts){\n unique.push_back(p.first);\n }\n n = unique.size();\n set<vector<int> > st;\n for(int i = 0; i < n; i++){\n for(int j = i; j < n; j++){\n int num1 = unique[i];\n int num2 = unique[j];\n counts[num1]--;\n if(counts[num2] <= 0){\n counts[num1]++;\n continue;\n }\n counts[num2]--;\n int num3 = -(num1 + num2);\n if(counts.find(num3) != counts.end() && counts[num3] > 0){\n vector<int> arr = {num1, num2, num3};\n sort(arr.begin(), arr.end());\n st.insert(arr);\n }\n counts[num1]++;\n counts[num2]++;\n }\n }\n for(vector<int> v : st){\n ans.push_back(v);\n }\n return ans;\n }\n};",
"memory": "46900"
} |
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 unordered_map<int, int> m;\n for (int i=0; i!=nums.size(); i++) m[-nums[i]]=i;\n vector<vector<int>> ans;\n unordered_set<long> n;\n for (int i=0; i!=nums.size(); i++)\n {\n if (nums[i]>0) break;\n for (int j=i+1; j<nums.size(); j++)\n {\n if (nums[j]+nums[i]>0) break;\n if (/*m.count(nums[i]+nums[j])>0&&*/m[nums[i]+nums[j]]>j)\n {\n long a=nums[i];\n long b=nums[j];\n n.insert((a+100000)*1000000+(b+100000));\n }\n }\n }\n for (auto i=n.begin(); i!=n.end(); i++)\n {\n int a=(int)(*i/1000000-100000);\n int b=(int)(*i%1000000-100000);\n ans.push_back({a, b, 0-a-b});\n }\n return ans;\n }\n};",
"memory": "51811"
} |
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 sort(nums.begin(), nums.end());\n map<int, bool> done;\n map<vector<int>, bool> doneSub;\n for (int i = 0; i < nums.size(); i++) { \n if(done.find(nums[i])==done.end()){\n done[nums[i]] = true;\n int lo = i+1;\n int hi = nums.size() - 1;\n int n = nums[i];\n map<int,bool> tempDone ={};\n while (lo < hi) {\n if (lo == i) {\n lo += 1;\n } \n else if (hi == i) {\n hi -= 1;\n }\n else {\n if(tempDone.find(nums[lo])!=tempDone.end()){ \n lo+=1;\n }\n else if (tempDone.find(nums[hi])!=tempDone.end()){\n hi-=1;\n }\n else{\n \n int sum = nums[lo] + nums[hi] + n;\n if (sum == 0) {\n vector<int> cAns;\n cAns.push_back(nums[lo]);\n cAns.push_back(nums[hi]);\n cAns.push_back(n);\n // sort(cAns.begin(), cAns.end());\n tempDone[nums[hi]] = true;\n tempDone[nums[lo]] = true;\n lo+=1;\n hi-=1;\n if (doneSub.find(cAns) == doneSub.end()) {\n ans.push_back(cAns);\n doneSub[cAns] = true;\n }\n \n } else if (sum < 0) {\n lo += 1;\n } else {\n hi -= 1;\n }\n }\n }\n }\n \n }}\n return ans;\n }\n};",
"memory": "51811"
} |
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 unordered_map<int, int> counts;\n set<vector<int>> ans;\n \n void twoSum(int target) {\n for (auto it = begin(counts); it != end(counts); ++it) {\n auto& [num, count] = *it;\n if (count == 0) { continue; }\n \n --counts[num];\n \n int complement = target - num;\n auto it2 = counts.find(complement);\n if (it2 != end(counts) && it2->second > 0) {\n vector<int> triplet = {-target, num, complement};\n sort(begin(triplet), end(triplet));\n ans.insert(triplet);\n }\n \n ++counts[num];\n }\n }\n \n vector<vector<int>> threeSum(vector<int>& nums) {\n for (int num : nums) {\n ++counts[num];\n }\n for (int num : nums) {\n --counts[num];\n twoSum(-num);\n ++counts[num];\n }\n \n return {begin(ans), end(ans)};\n }\n};",
"memory": "56723"
} |
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>& v, int start, int end, int target) {\n vector<pair<int, int>>res;\n unordered_map<string, bool> mp;\n int left = start;\n int right = end;\n while (left < right) {\n int currentTarget = v[left] + v[right];\n if (currentTarget == target) {\n pair<int, int>p;\n p.first=left;\n p.second=right;\n string s = to_string(left)+','+to_string(right);\n if(!mp[s]){\n res.push_back(p);\n mp[s]=true;\n }\n left++;\n right--;\n }\n else if (currentTarget > target)\n right--;\n else\n left++;\n }\n return res;\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n unordered_map<string, bool> mp;\n unordered_map<int, bool> sp;\n vector<vector<int>> res;\n sort(nums.begin(), nums.end());\n int n = nums.size();\n for (int i = 0; i < n - 2; i++) {\n if(sp[nums[i]]) continue;\n vector<pair<int,int>> ts = twoSum(nums, i + 1, n - 1, -1 * nums[i]);\n int m = ts.size();\n for(int j=0; j<m; j++){\n pair<int, int>pr = ts[j];\n string s = to_string(nums[i]) + ',' + to_string(nums[pr.first]) +\n ',' + to_string(nums[pr.second]);\n if (!mp[s]) {\n vector<int> v = {nums[i], nums[pr.first], nums[pr.second]};\n res.push_back(v);\n mp[s] = true;\n }\n }\n sp[nums[i]] = true;\n }\n return res;\n }\n};",
"memory": "56723"
} |
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>> outside;\n int low = 0;\n map<int, int> mp;\n for (int i = 0; i < nums.size(); i++) {\n mp[nums[i]]++;\n }\n vector<int> v1={0,0,0};\n vector<vector<int>> outside1={v1};\n if(mp[0]==nums.size())return outside1; \n sort(nums.begin(), nums.end());\n map<vector<int>, int> Duplicate;\n int high = nums.size() - 1;\n while (nums[low]<0 and low<=high) {\n if(nums[low]==nums[low+1]){\n low++;\n continue;\n }\n high=nums.size() - 1;\n while(nums[high]>0 and high>=low)\n {\n int sum = 0;\n vector<int> v;\n sum += (nums[low] + nums[high]);\n mp[nums[low]]--;\n mp[nums[high]]--;\n int oppSign = sum * (-1); \n if (mp[oppSign]) {\n v.push_back(oppSign);\n v.push_back(nums[low]);\n v.push_back(nums[high]);\n sort(v.begin(),v.end());\n if(Duplicate[v]==0){\n outside.push_back(v);\n Duplicate[v]++;\n }\n }\n mp[nums[low]]++;\n mp[nums[high]]++;\n high--;\n }\n low++; \n }\n \n if(mp[0]>=3)outside.push_back(v1);\n return outside;\n }\n};",
"memory": "81279"
} |
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<int> tmp;\n set<vector<int>> s;\n unordered_map<int, vector<int>> m;\n int j = 0;\n for(int i = 0; i < nums.size(); i++) {\n // we don't need to store more than 3 of the same number\n // as duplicate triplets are not allowed\n if(m[nums[i]].size() > 3) continue;\n\n m[nums[i]].push_back(j);\n j++;\n tmp.push_back(nums[i]);\n }\n nums = tmp;\n int missing;\n for(int i = 0; i < nums.size() - 2; i++) {\n for(int j = i+1; j < nums.size() - 1; j++) {\n missing = -1 * (nums[i] + nums[j]);\n if(m.find(missing)==m.end()) {\n continue;\n }\n\n for(auto& k: m[missing]) {\n if(k != i && k!= j) {\n // cout << i << \", \" << j << \", \" << k << \" with \" << nums[i] << nums[j] << nums[k] << endl;\n vector<int> v = {nums[i], nums[j], missing};\n sort(v.begin(), v.end());\n s.insert(v);\n }\n }\n }\n }\n\n vector<vector<int>> ans;\n for(auto& it: s) {\n ans.push_back(it);\n }\n\n return ans;\n }\n};",
"memory": "81279"
} |
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 sort(nums.begin(),nums.end());\n unordered_map<int,int> left,right;\n left[nums[0]]++;\n for(int i=2;i<nums.size();i++){\n right[nums[i]]++;\n }\n for(int i=1;i<nums.size()-1;i++){\n int sum=-nums[i];\n /*cout<<i<<endl;\n for(auto it:left){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n cout<<\"*\"<<endl;\n for(auto it:right){\n cout<<it.first<<\" \"<<it.second<<endl;\n }\n cout<<\"sum:\"<<sum<<endl;*/\n for(auto it:left){\n //cout<<it.first<<\" \"<<it.second<<\" \"<<sum-it.first<<endl;\n //cout<<right[sum-it.first]<<endl;\n if(it.second>0 and right[sum-it.first]>0){\n vector<int> vec={it.first,nums[i],sum-it.first};\n /*for(auto j:vec){\n cout<<j<<\" \";\n }cout<<endl;*/\n ans.push_back(vec);\n }\n }\n left[nums[i]]++;\n right[nums[i+1]]--;\n }\n sort(ans.begin(), ans.end());\n ans.erase(unique(ans.begin(), ans.end()), ans.end());\n return ans;\n }\n};",
"memory": "86190"
} |
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 vector<int>v(3);\n sort(nums.begin(),nums.end());\n map<vector<int>,bool>b;\n // cout<<nums[0];\n unordered_map<int,int>m;\n //check for \n for(int i=0;i<nums.size();i++){\n\n m[nums[i]]++;\n }\nint i=0;\nint j=nums.size()-1;\nfor(int i=0;i<nums.size()-2;i++){\n for(int j=i+1;j<nums.size();j++){\n//cout<<i<<\" \"<<j;\nint p=-nums[i]-nums[j];\n v[0]=nums[i];\n v[1]=nums[j];\n v[2]=p;\n//cout<<nums[i]<<\" \"<<nums[j]<<\" \"<<p<<\" \"<<\"\\n\";\n \n sort(v.begin(),v.end());\nif(m[p]>0){\nif(nums[i]==p&&nums[i]!=0){cout<<\"bue\";\n if(m[nums[i]]>1){\n b[v]=true;\n }\n}\nelse if(nums[j]==p&&nums[j]!=0){\n\n if(m[nums[j]]>1){\n b[v]=true;\n }\n\n}\nelse if(nums[i]==nums[j]&&nums[i]==0){\n//cout<<\"gg\";\nif(m[0]>2){\n \n b[v]=true;\n}}\nelse{ b[v]=true;\n}\n\n\n}\n\n}\nwhile(i!=nums.size()-2){\nif(nums[i]==nums[i+1]){\ni++;}\nelse{\n break;\n}\n}}\n\n\n map<vector<int>, bool>::iterator it = b.begin();\n\nwhile(it!=b.end()){\n\n \n vector<int>temp(3);\ntemp[0]=it->first[0];\n temp[1]=it->first[1];\n temp[2]=it->first[2];\n ans.push_back(temp);\n\n \n it++;\n}\nreturn ans;\n}\n \n \n};",
"memory": "86190"
} |
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 <set>\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n\n set<vector<int>> solution;\n for (int i = 0; i < nums.size() - 2; i++)\n {\n int j = i + 1;\n int k = nums.size() - 1;\n while(j < k)\n {\n int calc = nums[i] + nums[j] + nums[k];\n if (calc == 0)\n {\n vector<int> triplets = {nums[i], nums[j], nums[k]};\n sort(triplets.begin(), triplets.end());\n solution.insert(triplets);\n \n while(j < k && nums[j] == nums[j + 1])\n {\n j++;\n }\n \n k--;\n }\n else if (calc < 0)\n {\n j++;\n }\n else\n {\n k--;\n }\n }\n }\n\n vector<vector<int>> ret;\n\n for (auto vec : solution )\n {\n ret.push_back(vec);\n }\n\n return ret;\n }\n};",
"memory": "91101"
} |
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 <set>\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n\n set<vector<int>> solution;\n for (int i = 0; i < nums.size() - 2; i++)\n {\n int j = i + 1;\n int k = nums.size() - 1;\n while(j < k)\n {\n int calc = nums[i] + nums[j] + nums[k];\n if (calc == 0)\n {\n vector<int> triplets = {nums[i], nums[j], nums[k]};\n sort(triplets.begin(), triplets.end());\n solution.insert(triplets);\n \n while(j < k && nums[j] == nums[j + 1])\n {\n j++;\n }\n \n k--;\n }\n else if (calc < 0)\n {\n int curNumber = nums[j];\n do \n {\n j++;\n }\n while(j < k && curNumber == nums[j]);\n }\n else\n {\n k--;\n }\n }\n }\n\n vector<vector<int>> ret;\n\n for (auto vec : solution )\n {\n ret.push_back(vec);\n }\n\n return ret;\n }\n};",
"memory": "91101"
} |
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,j,k;\n set<vector<int>>st;\n int n=nums.size();\n sort(nums.begin(),nums.end());\n if(nums.front()>=0 && nums.back()<=0){\n return {{0,0,0}};\n }\n if(nums.front()>0 && nums.back()>0){\n return {};\n }\n if(nums.front()<0 && nums.back()<0){\n return {};\n }\n for(i=0;i<n;i++){\n j=i+1;\n k=n-1;\n while(j<k){\n int sum=nums[i]+nums[j]+nums[k];\n if(sum==0){\n vector<int>temp={nums[i],nums[j],nums[k]};\n st.insert(temp);\n j++;\n k--;\n }\n else if(sum<0){\n j++;\n }\n else{\n k--;\n }\n }\n }\n vector<vector<int>>res(st.begin(),st.end());\n return res;\n st.clear();\n }\n};",
"memory": "96013"
} |
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 if(nums.back()<=0&&nums.front()>=0)return {{0,0,0}};\n if(nums.back()<0&&nums.front()<0) return {};\n if(nums.back()>0&&nums.front()>0) return {};\n\n int n = nums.size();\n set<vector<int>> answer;\n for(int i=0; i<=n-3; i++) {\n // cout<< \"- \";\n int j= i+1;\n int k= n-1;\n while(k>j) {\n // cout<< \"here \";\n int sum = nums[i] + nums[j] + nums[k];\n if(sum > 0) k--;\n else if(sum < 0) j++;\n else {\n vector<int> x({nums[i], nums[j], nums[k]});\n answer.insert(x);\n j++; k--;\n }\n }\n }\n vector<vector<int>> result(answer.begin(),answer.end());\n return result;\n }\n};",
"memory": "96013"
} |
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>> output;\n map<int,int> mid; //required number\n unordered_map<long long,bool> end;\n unordered_map<int,int> exists; //number of occurences\n unordered_map<int,int> start; //number of occurences\n \n for(int i=0;i<nums.size();i++){\n exists[nums[i]]++;\n start[nums[i]]++;\n }\n for(auto it=start.begin();it!=start.end();++it){\n int num = -it->first;\n int testNum;\n for(int i=0;i<nums.size();i++){\n testNum = num-nums[i];\n if(exists[num-nums[i]]>0){\n vector<int> t = {-num,nums[i],num-nums[i]};\n bool push = false;\n if(-num==nums[i]&&num-nums[i]==-num){\n if(exists[nums[i]]>2){\n push=true;\n }\n }\n else if(num-nums[i]==nums[i]){\n if(exists[nums[i]]>1){\n push=true;\n }\n }else if(-num == nums[i]){\n if(exists[nums[i]]>1){\n push=true;\n }\n }else if(-num == num-nums[i]){\n if(exists[-num]>1){\n push=true;\n }\n }else{\n push=true;\n }\n if(push){\n sort(t.begin(),t.end());\n if(!end[(long long)(t[0])*100000+t[1]]){\n output.push_back(t);\n end[(long long)(t[0])*100000+t[1]] = true;\n }\n }\n }\n }\n }\n return output;\n }\n};",
"memory": "100924"
} |
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 // num -> indexes\n unordered_map<int, vector<int>> indexes;\n sort(nums.begin(), nums.end());\n\n for (auto i = 0; i < nums.size(); ++i) {\n indexes[nums[i]].push_back(i);\n }\n\n vector<vector<int>> result;\n unordered_set<string> result_strings;\n\n int prev_i = std::numeric_limits<int>::min();\n for (int i = 0; i < nums.size(); ++i) {\n\n if (nums[i] == prev_i) continue;\n else prev_i = nums[i];\n\n int prev_ii = std::numeric_limits<int>::min();\n for (int ii = i + 1; ii < nums.size(); ++ii) {\n\n if (nums[ii] == prev_ii) continue;\n else prev_ii = nums[ii];\n\n int rest = 0 - nums[i] - nums[ii];\n\n int prev_ind = -1;\n for (auto ind : indexes[rest]) {\n\n if (ind <= i || ind <= ii) continue;\n\n if (prev_ind != -1 && nums[ind] == nums[prev_ind]) continue;\n else prev_ind = ind;\n\n vector<int> candidate = {nums[i], nums[ii], nums[ind]};\n result.push_back(candidate);\n }\n }\n }\n\n return result;\n }\n\n bool is_duplicate(vector<int>& candidate, unordered_set<string>& result_strings) {\n sort(candidate.begin(), candidate.end());\n string str;\n char delim = ',';\n for (auto el : candidate) {\n str += (std::to_string(el));\n str.push_back(delim);\n }\n if (result_strings.contains(str)) return true;\n else {\n result_strings.insert(str);\n return false;\n }\n }\n\n};",
"memory": "100924"
} |
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{\nprivate:\n void check_if_duplicate(int a, int b, int c, vector<vector<int>>& ans, unordered_map<string, int>& ans_map)\n {\n vector<int> vec = {a, b, c};\n sort(vec.begin(), vec.end());\n string s = to_string(vec[0]) + \"/\" + to_string(vec[1]) + \"/\" + to_string(vec[2]);\n\n if(ans_map.find(s) == ans_map.end())\n {\n ans.push_back(vec);\n ans_map[s] = 0;\n }\n }\npublic:\n vector<vector<int>> threeSum(vector<int>& nums){\n vector<vector<int>> ans;\n unordered_map<string, int> ans_map;\n unordered_map<int, vector<int>> nums_map;\n int x, left = 0, right = nums.size()-1;\n\n sort(nums.begin(), nums.end());\n\n for(int i = 0; i < nums.size(); i++)\n nums_map[nums[i]].push_back(i);\n\n while(left < nums.size() && nums[left] < 0)\n {\n while(nums[right] > 0)\n {\n if(nums[right] + nums[left] == 0)\n x = 0;\n else\n x = -(nums[right] + nums[left]);\n \n if(nums_map.find(x) != nums_map.end())\n for(int k: nums_map[x])\n if(k != left && k != right)\n {\n check_if_duplicate(nums[left], nums[right], x, ans, ans_map);\n break;\n }\n right--;\n }\n right = nums.size()-1;\n left++;\n }\n\n if(left < nums.size() && left+1 < nums.size() && left+2 < nums.size())\n if(nums[left] == 0 && nums[left+1] == 0 && nums[left+2] == 0)\n {\n vector<int> vec = {0,0,0};\n ans.push_back(vec);\n }\n return ans;\n }\n};",
"memory": "105835"
} |
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 unordered_map<int,vector<int>>mp;\n int tmp=0;\n vector<vector<int>>ans;\n for(auto it:nums){\n mp[it].push_back(tmp);\n tmp++;\n }\n int n = nums.size();\n for(int i=0;i<n;i++){\n if(i>0 and nums[i]==nums[i-1])\n continue;\n for(int j=i+1;j<n;j++){\n if(j>(i+1) and nums[j]==nums[j-1])\n continue;\n int adder = nums[i]+nums[j];\n int rem = ((-1)*adder);\n if(mp[rem].size() >0){\n for(auto it:mp[rem])\n {\n if(it>i and it>j)\n {\n vector<int>tmpvec;\n tmpvec.push_back(nums[i]);\n tmpvec.push_back(nums[j]);\n tmpvec.push_back(rem);\n ans.push_back(tmpvec);\n break;\n }\n }\n }\n }\n }\n return ans;\n }\n};",
"memory": "105835"
} |
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<int>nums,int i,int j,int target,vector<vector<int>>&v){\n while(i<j){\n if(nums[i]+nums[j]<target) i++;\n else if(nums[i]+nums[j]>target) j--;\n else{\n v.push_back({nums[i],nums[j],-target});\n while(i<j && nums[i+1]==nums[i]) i++;\n while(i<j && nums[j]==nums[j-1]) j--;\n i++;j--;\n }\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<vector<int>>v;\n for(int i=0;i<nums.size()-2;i++){\n int target = -nums[i];\n if(i>0 && nums[i]==nums[i-1]) continue;\n twoSum(nums,i+1,nums.size()-1,target,v);\n }return v;\n }\n};",
"memory": "110746"
} |
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<int>nums,int i,int j,int target,vector<vector<int>>&v){\n while(i<j){\n if(nums[i]+nums[j]<target) i++;\n else if(nums[i]+nums[j]>target) j--;\n else{\n v.push_back({nums[i],nums[j],-target});\n while(i<j && nums[i+1]==nums[i]) i++;\n while(i<j && nums[j]==nums[j-1]) j--;\n i++;j--;\n }\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n if(nums.size()<3) return {};\n sort(nums.begin(),nums.end());\n vector<vector<int>>v;\n for(int i=0;i<nums.size()-2;i++){\n int target = -nums[i];\n if(i>0 && nums[i]==nums[i-1]) continue;\n twoSum(nums,i+1,nums.size()-1,target,v);\n }return v;\n }\n};",
"memory": "110746"
} |
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:\nvector<vector<int>>vec;\nvector<int>v;\nvoid solve(int i, int j, int target, vector<int>nums){\n int val = nums[i-1];\n while(i<j){\n if(nums[i]+nums[j]+val < 0) i++;\n else if(nums[i]+nums[j]+val > 0) j--;\n else{\n v.push_back(val);\n v.push_back(nums[i]);\n v.push_back(nums[j]);\n while(i<j && nums[i]==nums[i+1]) i++;\n while(i<j && nums[j]==nums[j-1]) j--;\n i++; j--; \n }\n if(v.size() > 0) vec.push_back(v);\n v.clear();\n }\n}\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n for(int i=0; i<nums.size(); i++){\n if(i==0 || nums[i]!=nums[i-1]){\n solve(i+1, n-1, 0, nums);\n }\n }\n return vec;\n }\n};",
"memory": "115658"
} |
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:\nvector<vector<int>>vec;\nvector<int>v;\nvoid solve(int i, int j, int target, vector<int>nums){\n int val = nums[i-1];\n while(i<j){\n if(nums[i]+nums[j] < target) i++;\n else if(nums[i]+nums[j] > target) j--;\n else{\n v.push_back(val);\n v.push_back(nums[i]);\n v.push_back(nums[j]);\n while(i<j && nums[i]==nums[i+1]) i++;\n while(i<j && nums[j]==nums[j-1]) j--;\n i++; j--; \n }\n if(v.size() > 0) vec.push_back(v);\n v.clear();\n }\n}\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n for(int i=0; i<nums.size(); i++){\n if(i==0 || nums[i]!=nums[i-1]){\n solve(i+1, n-1, 0-nums[i], nums);\n }\n }\n return vec;\n }\n};",
"memory": "115658"
} |
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>> res;\n\n sort(nums.begin(), nums.end());\n int n= nums.size();\n int prev = INT_MIN ;\n unordered_map<string,int> mp; \n for(int i=0; i<n; i++){\n if(prev == nums[i]){\n continue;\n }\n if(prev == INT_MIN){\n prev = nums[i];\n }\n\n for(int j = i+1, k = n-1; j<k; ){\n int sum = nums[i]+ nums[j]+nums[k];\n\n if(sum == 0){\n vector<int> a;\n a.push_back(nums[i]);\n a.push_back(nums[j]);\n a.push_back(nums[k]);\n \n stringstream oss;\n for (int i : a) {\n oss << i << \" \";\n }\n std::string vectorStr = oss.str();\n vectorStr.pop_back(); // Remove the trailing space\n if(mp.find(vectorStr) == mp.end()){\n mp[vectorStr] = 0;\n res.push_back(a);\n }\n\n k--;\n j++;\n }\n else if(sum>0){\n k--;\n }\n else if(sum<0){\n j++;\n }\n }\n }\n\n return res;\n\n \n }\n};",
"memory": "120569"
} |
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>> res;\n\n sort(nums.begin(), nums.end());\n int n= nums.size();\n int prev = INT_MIN ;\n unordered_map<string,int> mp; \n for(int i=0; i<n; i++){\n if(prev == nums[i]){\n continue;\n }\n if(prev == INT_MIN){\n prev = nums[i];\n }\n\n for(int j = i+1, k = n-1; j<k; ){\n int sum = nums[i]+ nums[j]+nums[k];\n\n if(sum == 0){\n vector<int> a;\n a.push_back(nums[i]);\n a.push_back(nums[j]);\n a.push_back(nums[k]);\n \n stringstream oss;\n for (int i : a) {\n oss << i << \" \";\n }\n std::string vectorStr = oss.str();\n vectorStr.pop_back(); // Remove the trailing space\n if(mp.find(vectorStr) == mp.end()){\n mp[vectorStr] = 0;\n res.push_back(a);\n }\n\n k--;\n j++;\n }\n else if(sum>0){\n k--;\n }\n else if(sum<0){\n j++;\n }\n }\n }\n\n return res;\n\n \n }\n};",
"memory": "120569"
} |
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 <unordered_map>\n#include <vector>\n#include <set>\n#include <climits>\n\nclass Solution {\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n // Key: Number, value: vector of indexes of the number\n unordered_map<int, vector<int>> umap;\n // Set of sets (triplets), which prevents duplicates\n set<vector<int>> triplets;\n // Result\n vector<vector<int>> res;\n\n vector<int> newNums;\n sort(nums.begin(), nums.end());\n\n int current = nums.at(0), count = 1;\n newNums.push_back(nums.at(0));\n for (int i = 1; i < nums.size(); i++) {\n if (current != nums.at(i)) {\n current = nums.at(i);\n count = 1;\n }\n else {count ++;}\n if (count <= 3) {newNums.push_back(nums.at(i));}\n }\n\n for (int i = 0; i < newNums.size(); i++) {\n if (umap.find(newNums.at(i)) != umap.end()) {\n umap[newNums.at(i)].push_back(i);\n }\n else {\n umap.insert({newNums.at(i), {i}});\n }\n }\n for (int i = 0; i < newNums.size(); i++) {\n for (int j = i + 1; j < newNums.size(); j++) {\n int third = (newNums.at(i) + newNums.at(j)) * -1;\n\n if (umap.find(third) != umap.end()) {\n for (int index : umap[third]) {\n if (index != i && index != j) {\n vector<int> triplet;\n triplet.push_back(newNums.at(i));\n triplet.push_back(newNums.at(j));\n triplet.push_back(newNums.at(index));\n sort(triplet.begin(), triplet.end());\n triplets.insert(triplet);\n }\n }\n }\n }\n }\n for (vector<int> triplet : triplets) {\n vector<int> v = {};\n for (int num : triplet) {\n v.push_back(num);\n }\n res.push_back(v);\n }\n return res;\n }\n};",
"memory": "125480"
} |
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 <unordered_map>\n#include <vector>\n#include <set>\n#include <climits>\n\nclass Solution {\npublic:\n void sort(vector<int>& arr) {\n for (int i = 0; i < arr.size() - 1; i++) {\n int minIndex = i;\n for (int j = i + 1; j < arr.size(); j++) {\n if (arr.at(j) < arr.at(minIndex)) {minIndex = j;}\n }\n int temp = arr.at(minIndex);\n arr.at(minIndex) = arr.at(i);\n arr.at(i) = temp;\n }\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n // Key: Number, value: vector of indexes of the number\n unordered_map<int, vector<int>> umap;\n // Set of sets (triplets), which prevents duplicates\n set<vector<int>> triplets;\n // Result\n vector<vector<int>> res;\n\n vector<int> newNums;\n std::sort(nums.begin(), nums.end());\n\n int current = nums.at(0), count = 1;\n newNums.push_back(nums.at(0));\n for (int i = 1; i < nums.size(); i++) {\n if (current != nums.at(i)) {\n current = nums.at(i);\n count = 1;\n }\n else {count ++;}\n if (count <= 3) {newNums.push_back(nums.at(i));}\n }\n\n for (int i = 0; i < newNums.size(); i++) {\n if (umap.find(newNums.at(i)) != umap.end()) {\n umap[newNums.at(i)].push_back(i);\n }\n else {\n umap.insert({newNums.at(i), {i}});\n }\n }\n for (int i = 0; i < newNums.size(); i++) {\n for (int j = i + 1; j < newNums.size(); j++) {\n int third = (newNums.at(i) + newNums.at(j)) * -1;\n\n if (umap.find(third) != umap.end()) {\n for (int index : umap[third]) {\n if (index != i && index != j) {\n vector<int> triplet;\n triplet.push_back(newNums.at(i));\n triplet.push_back(newNums.at(j));\n triplet.push_back(newNums.at(index));\n sort(triplet);\n triplets.insert(triplet);\n }\n }\n }\n }\n }\n for (vector<int> triplet : triplets) {\n vector<int> v = {};\n for (int num : triplet) {\n v.push_back(num);\n }\n res.push_back(v);\n }\n return res;\n }\n};",
"memory": "125480"
} |
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>& vec) {\n \n // sort(vec.begin(),vec.end());\n set<int>s;\n unordered_map<int,int>mp;\n vector<int>temp;\n set<multiset<int>> ans;\n vector<vector<int>> ansf;\n for(auto xy:vec)\n {\n if(mp[xy]==0)\n temp.push_back(xy);\n mp[xy]++;\n }\n sort(temp.begin(),temp.end());\n int n=temp.size();\n for(int i=0;i<n;i++)\n {\n for(int j=i;j<n;j++)\n {\n if(i==j && mp[temp[i]]<2)\n continue;\n int t=-1*(temp[i]+temp[j]);\n if(mp[t]>0)\n {\n if(t==temp[i] && t==temp[j] )\n {\n if(mp[t]>=3)\n ans.insert({temp[i],temp[j],t,});\n }\n else if(temp[i]==temp[j])\n ans.insert({temp[i],temp[j],t});\n else if((t==temp[i] || t==temp[j]))\n {\n if(mp[t]>=2)\n ans.insert({temp[i],temp[j],t});\n }\n else \n ans.insert({temp[i],temp[j],t});\n }\n }\n }\n int c=0;\n for(auto xy:ans)\n {\n ansf.push_back({});\n for(auto yz:xy)\n {\n // cout<<yz<<\" \";\n ansf[c].push_back(yz);\n }\n // cout<<\"\\n\";\n c++;\n }\n return ansf;\n }\n};",
"memory": "130391"
} |
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>> ans;\n set <vector<int>> triplets;\n int n = nums.size();\n unordered_map <int, int> freq;\n for(int i = 0;i<n;i++)\n {\n freq[nums[i]]++;\n }\n if(nums[0] == 0 && freq[0]>=3) return {{0,0,0}};\n if(nums[0] > 0) return {};\n int f, s, t;\n for(int i = 0;i<n;i++)\n {\n f = nums[i];\n freq[f]--;\n for(int j = i + 1;j<n;j++)\n {\n s = nums[j];\n freq[s]--;\n t = -f -s;\n if(t>=s && freq[t] > 0)\n {\n vector<int> temp = {f,s,t};\n //sort(temp.begin(), temp.end());\n triplets.insert(temp);\n }\n freq[s]++;\n }\n freq[f]++;\n }\n for(auto it: triplets)\n {\n ans.push_back(it);\n }\n return ans;\n }\n};",
"memory": "130391"
} |
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(int target, int low, vector<int>nums){\n vector<vector<int>>arr;\n int high = nums.size()-1;\n while(low<high){\n int sum = nums[low]+nums[high];\n if(sum==target){\n arr.push_back({low, high});\n low+=1;\n high-=1;\n }else if(sum<target){\n low+=1;\n }else{\n high-=1;\n }\n }\n return arr;\n }\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n set<vector<int>>records;\n for(int i=0; i<nums.size(); i+=1){\n if(i!=0 && nums[i]==nums[i-1]){\n continue;\n }\n auto pairs = twoSum(-nums[i], i+1, nums);\n for(auto pair: pairs){\n vector<int>indexes{nums[i], nums[pair[0]], nums[pair[1]]};\n sort(indexes.begin(), indexes.end());\n records.insert(indexes);\n }\n }\n vector<vector<int>>result;\n for(auto record: records){\n result.push_back(record);\n }\n return result;\n }\n};",
"memory": "135303"
} |
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 {\nprivate:\n vector<int> nums;\n\n vector<vector<int>> twoSum(int begin, int end, int target, vector<int> nums) {\n int left = begin;\n int right = end;\n int base = nums[left] + nums[right];\n vector<vector<int>> sol_set;\n\n while (left < right) {\n if (base < target) {\n left++;\n } else if (base > target) {\n right--;\n } else {\n sol_set.push_back({nums[left], nums[right]});\n left++;\n right--;\n while (left < right && nums[left] == nums[left - 1]) left++;\n }\n base = nums[left] + nums[right];\n }\n\n return sol_set;\n }\n\npublic:\n vector<vector<int>> threeSum(vector<int>& nums) {\n vector<vector<int>> res;\n\n sort(nums.begin(), nums.end());\n int base = nums[0];\n for (int i = 0; i < nums.size() - 2; i++) {\n if (i > 0 && nums[i] == base) {\n continue;\n }\n vector<vector<int>> sub_res = twoSum(i + 1, nums.size() - 1, 0 - nums[i], nums);\n if (sub_res.empty()) {\n continue;\n }\n for (auto s : sub_res) {\n s.push_back(nums[i]);\n res.push_back(s);\n }\n base = nums[i];\n }\n\n return res;\n }\n};",
"memory": "135303"
} |
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:\nvector<vector<int>> threeSum(vector<int>& nums) {\n \n unordered_map<int, int> map;\n vector<vector<int>> res;\n set<vector<int>> mySet;\n sort(nums.begin(), nums.end());\n\n if(nums[0] == nums[nums.size() - 1] && nums[0] == 0){\n res.push_back({0,0,0});\n return res;\n }\n\n for(int i=0; i<nums.size(); i++){\n map[nums[i]] = i + 1;\n }\n\n for(int i=0; i<nums.size(); i++){\n if(nums[i] > 0){\n return res;\n }\n for(int j=i+1; j<nums.size(); j++){\n int val = map[(-1) * (nums[i] + nums[j])];\n if(val != 0 && (val - 1) != i && (val - 1) > j && mySet.find({nums[i], nums[j], nums[val - 1]}) == mySet.end()){\n res.push_back({nums[i], nums[j], nums[val - 1]});\n mySet.insert({nums[i], nums[j], nums[val - 1]});\n }\n }\n }\n return res;\n }\n};",
"memory": "140214"
} |
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 multimap<int,int> m;\n set<vector<int>> s ;\n vector<vector<int>> v ;\n sort(nums.begin(),nums.end());\n for(int i = 0; i<nums.size();i++)\n {\n if(m.count(nums[i])<=3)\n m.insert({nums[i],i}) ;\n }\n\n for(auto x : m)\n {\n int twosum = x.first * (-1) ;\n for(auto y : m)\n {\n if(x.second != y.second)\n {\n int com = (twosum - y.first);\n auto it = m.find(com);\n if(it!=m.end() && it->second!=x.second && it->second!=y.second)\n {\n vector<int> sol;\n sol.push_back(x.first);\n sol.push_back(y.first);\n sol.push_back(it->first);\n sort(sol.begin(),sol.end());\n s.insert(sol);\n\n }\n }\n // else\n // {\n // continue ;\n // }\n }\n\n }\n\n for(auto p : s)\n {\n v.push_back(p);\n }\n\n return v ;\n }\n};",
"memory": "140214"
} |
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 vector<vector<int>> result{};\n sort(nums.begin(), nums.end());\n\n for (int i = 0; i+2 < nums.size(); ++i) {\n if (nums[i] > 0) {\n break;\n }\n int kprev = nums.size()-1;\n\n for (int j = i+1; j+1 < nums.size(); ++j) {\n int sum = nums[i] + nums[j];\n\n for (int k = kprev; k > j; --k) {\n int dif = sum + nums[k];\n\n if (dif < 0) {\n kprev = k;\n break;\n } else if (dif == 0) {\n vector<int> res {nums[i], nums[j], nums[k]};\n result.push_back(std::move(res));\n kprev = k;\n break;\n }\n\n if (nums[k] == 0) {\n break;\n }\n }\n\n }\n\n if (nums[i] == 0) {\n break;\n }\n\n }\n\n sort(result.begin(), result.end());\n auto last = unique(result.begin(), result.end());\n result.erase(last, result.end());\n\n return result;\n }\n};\n",
"memory": "145125"
} |
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": "const int OFFSET = 1e5;\n\nclass Solution {\npublic:\n int cnt[200001];\n void add(int val) {\n ++cnt[val + OFFSET];\n }\n void remove(int val) {\n --cnt[val + OFFSET];\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(begin(nums), end(nums));\n vector<vector<int>> res;\n set<vector<int>> S;\n for (auto x: nums) add(x);\n\n for (int i = 0; i< nums.size(); ++i) {\n remove(nums[i]);\n for (int j = i + 1; j < nums.size(); ++j) {\n remove(nums[j]);\n if ((abs(-nums[i] - nums[j]) <= 1e5) && cnt[-nums[i] - nums[j] + OFFSET] > 0) {\n vector<int> tmp = {nums[i], nums[j], -nums[i] - nums[j]};\n sort(begin(tmp), end(tmp));\n if (!S.count(tmp)) {\n res.push_back(tmp);\n S.insert(tmp);\n }\n if (tmp[0] == 0 && tmp[1] == 0 && tmp[2] == 0) {\n return res;\n }\n }\n add(nums[j]);\n }\n add(nums[i]);\n }\n return res;\n }\n};",
"memory": "145125"
} |
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>> st;\n sort(nums.begin(),nums.end());\n //If even after sorting there is no -ve number then 0 sum cant be made\n if(nums[0]>0) return {};\n\n int n=nums.size();\n for(int k=0;k<n;k++)\n {\n int ele=-nums[k];\n for(int i=k+1,j=n-1;i<j;)\n {\n if(nums[i]+nums[j]==ele)\n { \n st.push_back({nums[i],nums[j],nums[k]}); \n int x1=nums[j];\n int x2=nums[k];\n i++; j--;\n while(i<j&& nums[i]==x1) i++;\n while(i<j&& nums[j]==x2) j--;\n }\n else if(nums[i]+nums[j]>ele) j--;\n else i++;\n }\n }\n set<vector<int>> set(st.begin(),st.end());\n return vector<vector<int>>(set.begin(),set.end());\n }\n};",
"memory": "150036"
} |
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": "//first come to twoSum\n\nclass Solution {\npublic:\n\n vector<vector<int>> threeSum(vector<int>& n) \n {\n auto print = [&]()\n {\n for(auto nn : n)\n cout << nn << \" \";\n cout << endl;\n };\n\n std::sort(n.begin(), n.end());\n \n //value to indicies map for fast lookups in twoSum\n unordered_map<int, vector<int>> m;\n for(int i = 0; i < n.size(); i++)\n m[n[i]].push_back(i);\n \n\n auto twoSum = [&](int skip, int target) -> set<pair<int, int>>\n {\n set<pair<int, int>> r;\n for(int i = skip+1; i < n.size(); i++)\n {\n auto x = -(target + n[i]);\n if(auto it = m.find(x); it != m.end())\n {\n auto [_, jj] = *it;\n for(auto j : jj)\n if(j > i)\n r.insert({n[i], n[j]});\n }\n }\n return r;\n \n };\n\n if(n.size() < 2)\n return {};\n\n \n set<int> visited;\n vector<vector<int>> r;\n for(int i = 0; i < (int)n.size()-2; i++)\n {\n //target\n auto t = n[i];\n \n \n if(m[t].size() > 1)\n {\n //avoid duplication for the same target at different index\n if(visited.find(t) != visited.end())\n continue;\n visited.insert(t);\n }\n\n for(auto [j, k] : twoSum(i, t))\n {\n r.emplace_back(vector<int>{t, j, k});\n }\n }\n \n return r;\n }\n};",
"memory": "150036"
} |
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 <set>\nusing namespace std;\nclass Solution {\npublic:\n int binarySearch(int toFind, vector<int>& nums, int start){\n if(start < 0){\n start = 0;\n }\n\n int end = nums.size() - 1;\n while(start <= end){\n int index = start + (end - start) / 2;\n if(nums[index] < toFind){\n start = index + 1;\n }else if(nums[index] > toFind){\n end = index - 1;\n }\n else{\n return index;\n }\n }\n return -1;\n }\n\n\n vector<vector<int>> twoSum(int index, int num, vector<int>& nums, int min_value, int max_value){\n vector<vector<int>> pairs;\n for(int i=0; i<nums.size(); i++){\n if(i == index){\n continue;\n }\n\n int number = nums[i];\n int needed = num - number;\n if (needed < min_value || needed > max_value){\n continue;\n }\n \n int start_index = binarySearch(needed, nums, 0);\n if(start_index == -1){\n continue;\n }\n\n if(start_index == index){\n start_index = binarySearch(needed, nums, start_index+1);\n if(start_index == -1){\n continue;\n }\n }\n\n while(true){\n if(nums[start_index] == needed){\n if(start_index != i){\n vector<int> pair = {number, nums[start_index]};\n pairs.push_back(pair);\n }\n start_index++;\n }\n break;\n } \n }\n\n return pairs;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int min_value = nums[0];\n int max_value = nums[nums.size()-1];\n set<vector<int>> triplets;\n int current_num = min_value;\n bool success = false;\n\n for(int i=0; i<nums.size(); i++){\n int num = nums[i];\n if(num == current_num && success){\n continue;\n }\n current_num = num;\n success = false;\n vector<vector<int>> pairs = twoSum(i, -num, nums, min_value, max_value);\n if(pairs.size() > 0){\n for(int j=0; j<pairs.size(); j++){\n vector<int> pair = pairs[j];\n vector<int> triplet = {num, pair[0], pair[1]};\n sort(triplet.begin(), triplet.end());\n triplets.insert(triplet);\n success = true;\n }\n }\n\n }\n\n vector<vector<int>> ret;\n for(vector<int> triplet: triplets){\n ret.push_back(triplet);\n }\n return ret;\n }\n};",
"memory": "154948"
} |
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<int>temp(3);\n vector<vector<int>>ans;\n int pos=-1;\n for(int i=0;i<nums.size();i++){\n if(nums[i]>=0){\n pos=i;\n break;\n }\n }\n if(pos>=0){\n for(int i=0;i<=pos;i++){\n for(int j=i+1;j<nums.size()-1;j++){\n int lo=pos;\n if(j>=pos) lo=j+1;\n int hi=nums.size()-1;\n while(lo<=hi){\n int mid=lo+(hi-lo)/2;\n if(nums[mid]==-(nums[i]+nums[j])){\n temp[0]=nums[i];\n temp[1]=nums[j];\n temp[2]=nums[mid];\n ans.push_back(temp);\n break;\n }\n else if(nums[mid]>(-(nums[i]+nums[j]))) hi=mid-1;\n else lo=mid+1;\n }\n }\n }\n if(ans.size()>0){\n sort(ans.begin(),ans.end());\n stack<vector<int>>st;\n st.push(ans[0]);\n for(int i=1;i<ans.size();i++){\n if(st.top()!=ans[i]) st.push(ans[i]);\n }\n vector<vector<int>>finalAns;\n while(st.size()>0){\n finalAns.push_back(st.top());\n st.pop();\n }\n return finalAns;\n }\n else return ans;\n }\n else return ans;\n \n }\n};",
"memory": "154948"
} |
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>>st;\n for(int i=0;i<n-2;i++)\n {\n int k=i+1,j=n-1;\n while(k<j)\n {\n if(nums[k]+nums[j]==(-nums[i]))\n {\n st.insert({nums[i],nums[k],nums[j]});\n k++;\n j--;\n }\n else{\n if(nums[k]+nums[j]>(-nums[i]))\n j--;\n else\n k++;\n }\n \n\n }\n if(nums[i]==nums[i+1]) i++;\n }\n vector<vector<int>>vec(st.begin(),st.end());\n return vec;\n }\n};",
"memory": "159859"
} |
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>>s;\n sort(nums.begin(),nums.end());\n for(int i=0;i<n-2;i++){\n if(i>0 && nums[i]==nums[i-1])i++;\n int j=i+1,k=n-1;\n while(j<k){\n if(nums[i]+nums[j]+nums[k]==0){\n s.insert({nums[i],nums[j],nums[k]});\n j++;\n k--;\n }\n \n else if(nums[i]+nums[j]+nums[k]<0)j++;\n else k--;\n }\n }\n vector<vector<int>>ans(s.begin(),s.end());\n return ans;\n }\n};",
"memory": "159859"
} |
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 set<vector<int>> req;\n vector<vector<int>> ans;\n for(int x = 0;x<n;x++)\n {\n if(x != 0 && nums[x] == nums[x-1])\n x++;\n int i = x+1;\n int j = n-1;\n while(i<j)\n {\n if(nums[i]+ nums[j]+nums[x] == 0)\n {\n req.insert({nums[x],nums[i],nums[j]});\n i++;\n j--;\n }\n else if (nums[i]+ nums[j]+nums[x] >= 0)\n {\n j--;\n }\n else\n i++;\n\n }\n }\n\n for(auto triplets : req)\n ans.push_back(triplets);\n return ans;\n }\n};",
"memory": "164770"
} |
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<int> s;vector<vector<int>> ans;\n for(auto it : nums){\n s.insert(it);\n }\n if(s.size()==1 && nums[0]==0){\n vector<int> v;\n v={0,0,0};\n ans.push_back(v);\n return ans;\n }\n map<int,int> mp;\n sort(nums.begin(),nums.end());\n for(auto it: nums){\n mp[it]++;\n }\n set<vector<int>> st;\n for(int i=0;i<nums.size();i++){\n for(int j=i+1;j<nums.size();j++){\n int a=-(nums[i]+nums[j]);\n if(a<nums[i]){\n break;\n }\n mp[nums[i]]--;mp[nums[j]]--;\n if(mp.find(a)!=mp.end() && mp[a]>0){\n vector<int> v;\n v={nums[i],nums[j],a};\n sort(v.begin(),v.end());\n st.insert(v);\n }\n mp[nums[i]]++;mp[nums[j]]++;\n }\n }\n for(auto it : st){\n ans.push_back(it);\n }\n return ans;\n }\n}; ",
"memory": "164770"
} |
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 unordered_map<int,vector<int>> targets;\n for (int i = 0; i < nums.size(); i++) {\n targets[nums[i]].push_back(i);\n }\n\n vector<vector<int>> result;\n int last_ni = INT32_MIN;\n int last_nj = INT32_MIN;\n for (int i = 0; i < nums.size(); i++) {\n for (int j = i + 1; j < nums.size(); j++) {\n if ((last_ni == nums[i] && last_nj == nums[j]) || (last_ni == nums[j] && last_nj == nums[i])) {\n continue;\n }\n last_ni = nums[i];\n last_nj = nums[j];\n int t = -(nums[i] + nums[j]);\n for (int k : targets[t]) {\n if (i != k && j != k) {\n vector<int> triplet = {nums[i], nums[j], nums[k]};\n sort(triplet.begin(), triplet.end());\n result.emplace_back(triplet);\n break;\n }\n }\n }\n }\n\n if (result.size()) {\n sort(result.begin(), result.end());\n int w = 1;\n for (int r = 1; r < result.size(); r++) {\n auto& a = result[w - 1];\n auto& b = result[r];\n if (a[0] != b[0] || a[1] != b[1] || a[2] != b[2]) {\n if (w != r) {\n result[w] = move(result[r]);\n }\n w++;\n }\n }\n result.resize(w);\n }\n return result;\n }\n};",
"memory": "169681"
} |
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{\npublic:\n vector<vector<int>> GetTwoSum(vector<int> v, int index)\n {\n int t = 0 - (v[index]);\n int front = index + 1;\n int back = v.size() - 1;\n vector<vector<int>> res;\n while (front < back)\n {\n if (v[front] + v[back] == t)\n {\n vector<int> r;\n r.push_back (v[front]);\n r.push_back (v[back]);\n res.push_back (r);\n ++front;\n --back;\n }\n else if (v[front] + v[back] < t)\n {\n ++front;\n }\n else\n {\n --back;\n }\n }\n return res;\n }\n\n vector<int> RemoveDups(vector<int>& nums)\n {\n unordered_map <int, int> m;\n for (auto n : nums) \n {\n m[n]++;\n }\n nums.clear();\n for (auto e : m)\n {\n if (e.second > 3)\n {\n nums.push_back (e.first);\n nums.push_back (e.first);\n nums.push_back (e.first);\n }\n else\n {\n for (auto i = 0; i < e.second; ++i)\n {\n nums.push_back (e.first);\n }\n }\n }\n return nums;\n }\n\n vector<vector<int>> RemoveDups2(vector<vector<int>>& res)\n {\n set<vector<int>> s;\n for (auto r : res)\n {\n s.insert(r);\n }\n res.clear();\n for (auto e : s)\n {\n res.push_back (e);\n }\n return res;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) \n {\n vector<vector<int>> res;\n nums = RemoveDups(nums);\n \n sort (nums.begin(), nums.end());\n for (auto i = 0; i < nums.size() - 2; ++i)\n {\n vector<vector<int>> ts = GetTwoSum (nums, i);\n if (ts.size() != 0)\n {\n for (auto v : ts)\n {\n vector<int> r;\n r.push_back (nums[i]);\n r.push_back (v[0]);\n r.push_back (v[1]);\n res.push_back (r);\n }\n }\n }\n res = RemoveDups2(res);\n return res;\n }\n};",
"memory": "174593"
} |
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{\npublic:\n vector<vector<int>> GetTwoSum(vector<int> v, int index)\n {\n int t = 0 - (v[index]);\n int front = index + 1;\n int back = v.size() - 1;\n vector<vector<int>> res;\n while (front < back)\n {\n if (v[front] + v[back] == t)\n {\n vector<int> r;\n r.push_back (v[front]);\n r.push_back (v[back]);\n res.push_back (r);\n ++front;\n --back;\n }\n else if (v[front] + v[back] < t)\n {\n ++front;\n }\n else\n {\n --back;\n }\n }\n return res;\n }\n\n vector<int> RemoveDups(vector<int>& nums)\n {\n unordered_map <int, int> m;\n for (auto n : nums) \n {\n m[n]++;\n }\n nums.clear();\n for (auto e : m)\n {\n if (e.second > 3)\n {\n nums.push_back (e.first);\n nums.push_back (e.first);\n nums.push_back (e.first);\n }\n else\n {\n for (auto i = 0; i < e.second; ++i)\n {\n nums.push_back (e.first);\n }\n }\n }\n return nums;\n }\n\n vector<vector<int>> RemoveDups2(vector<vector<int>>& res)\n {\n set<vector<int>> s;\n for (auto r : res)\n {\n s.insert(r);\n }\n res.clear();\n for (auto e : s)\n {\n res.push_back (e);\n }\n return res;\n }\n\n vector<vector<int>> threeSum(vector<int>& nums) \n {\n vector<vector<int>> res;\n nums = RemoveDups(nums);\n for (auto e : nums)\n {\n cout << e << \" \"; \n }\n cout << endl;\n \n sort (nums.begin(), nums.end());\n for (auto i = 0; i < nums.size() - 2; ++i)\n {\n vector<vector<int>> ts = GetTwoSum (nums, i);\n if (ts.size() != 0)\n {\n for (auto v : ts)\n {\n vector<int> r;\n r.push_back (nums[i]);\n r.push_back (v[0]);\n r.push_back (v[1]);\n res.push_back (r);\n }\n }\n }\n res = RemoveDups2(res);\n return res;\n }\n};",
"memory": "174593"
} |
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 unordered_map<int,vector<int> > present;\n // map<vector<int>,int> check;\n\n vector<vector<int>> threeSum(vector<int>& nums) \n {\n int n = nums.size();\n\n bool check=true;\n\n for(int x:nums)\n {\n if(x!=0){check=false; break;}\n }\n if(check) return vector<vector<int>>{{0,0,0}};\n\n for(int i=0; i<nums.size(); i++)\n {\n present[nums[i]].push_back(i);\n }\n\n set<vector<int> > ans;\n\n\n for(int i=0; i<n; i++)\n {\n for(int j=i+1; j<n; j++)\n {\n int a = nums[i];\n int b = nums[j];\n int c = 0-a-b;\n vector<int> &x = present[c];\n auto last = upper_bound(x.begin(),x.end(),j);\n if(last!=x.end())\n {\n vector<int> ans1 = {a,b,c};\n sort(ans1.begin(),ans1.end());\n ans.insert(ans1);\n }\n }\n }\n\n vector<vector<int>> final;\n final.reserve(ans.size()*3);\n\n for(auto &v:ans) final.push_back(v);\n\n return final;\n }\n};",
"memory": "179504"
} |
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 len = nums.size();\n sort(nums.begin(),nums.end());\n int j,k;\n set<vector<int>> ans;\n for(int i=0;i<len-2;i++){\n if(nums[i]==nums[i+2]){\n if(nums[i]==0){\n vector<int> temp(3,0);\n ans.insert(temp);\n continue;\n }\n }\n j=i+1;\n k=len-1;\n while(j<k){\n \n int sum = nums[i]+nums[j]+nums[k];\n if(sum<0){\n j++;\n }else if(sum>0){\n k--;\n }else{\n vector<int> vect;\n vect.push_back(nums[i]);\n vect.push_back(nums[j]);\n vect.push_back(nums[k]);\n ans.insert(vect);\n j++;\n k--;\n \n }\n }\n \n\n }\n \n vector<vector<int>> Ans(ans.begin(),ans.end());\n return Ans;\n \n }\n};",
"memory": "184415"
} |
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 set<vector<int>> seen;\n\n sort(nums.begin(), nums.end());\n\n for (int i = 0; i < nums.size() - 2; i++){\n int left = i + 1;\n int right = nums.size() - 1;\n\n int l = nums[i];\n int m = nums[left];\n int r = nums[right];\n int s = l + m + r;\n while (left < right){\n m = nums[left];\n r = nums[right];\n s = l + m + r;\n if (s > 0){\n right -= 1;\n } else if (s < 0){\n left += 1;\n }\n if (s == 0 && left < right){\n vector<int> temp_res;\n temp_res.push_back(l);\n temp_res.push_back(m);\n temp_res.push_back(r);\n left += 1;\n right -= 1;\n if (!seen.contains(temp_res)){\n res.push_back(temp_res);\n seen.insert(temp_res);\n while (left < right && m == nums[left]){\n left +=1;\n }\n while (left < right && r == nums[right]){\n right -=1;\n }\n }\n }\n if (l == r){\n break;\n }\n }\n }\n return res;\n }\n};",
"memory": "184415"
} |
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>> findTwoSum(vector<int>& nums, int l, int r, int sum) {\n vector<vector<int>> res;\n while (l < r) {\n int temp = nums[l] + nums[r];\n if (temp == sum) {\n res.push_back({sum * -1, nums[l++], nums[r--]});\n } else if (temp > sum) {\n r--;\n } else {\n l++;\n }\n }\n return res;\n }\n \n vector<vector<int>> threeSum(vector<int>& nums) {\n if (all_of(nums.begin(), nums.end(), [&](const int i) { return i == 0; })) {\n return {{0, 0, 0}};\n }\n \n sort(nums.begin(), nums.end());\n set<vector<int>> result;\n vector<vector<int>> x;\n \n for (int i = 0; i < nums.size() - 2; i++) {\n vector<vector<int>> temp = findTwoSum(nums, i + 1, nums.size() - 1, nums[i] * -1);\n for (int j = 0; j < temp.size(); j++) {\n result.insert(temp[j]);\n }\n }\n \n for (auto it : result) {\n x.push_back(it);\n }\n \n return x;\n }\n};\n",
"memory": "189326"
} |
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:\nvector<vector<int>> threeSum(vector<int>& nums) {\n\n vector<vector<int> > fin;\n set<vector<int> > res;\n if(nums.size()<3)\n return fin;\n\n sort(nums.begin(), nums.end());\n int p=0;\n int q=1;\n int r=nums.size()-1;\n\n\n int posl=r;\n\n while(p+2<nums.size())\n {\n if(q>=r)\n {\n p++;\n q=p+1;\n r=nums.size()-1;\n if(p> r || p+2>=nums.size()) break;\n }\n\n int sum=nums[p]+nums[q]+nums[r];\n if(!sum)\n {\n vector<int> v;\n v.push_back(nums[p]);\n v.push_back(nums[q]);\n v.push_back(nums[r]);\n res.insert(v);\n q++;\n posl=r;\n if(nums[p]==nums[q] && nums[q]==nums[r]) break;\n continue;\n\n\n }\n if(sum>0)\n r--;\n else\n {\n q++;\n }\n\n\n\n\n }\n\n\n for(const auto& i : res)\n fin.push_back(i);\n \n \n return fin;\n\n}\n};",
"memory": "189326"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.