# 3Sum LeetCode Solution | Easy Approach | C++, Java, Python

Share:

3Sum LeetCode | Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j``i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.

Notice that the solution set must not contain duplicate triplets.

Example 1:

```Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
```

Example 2:

```Input: nums = []
Output: []
```

Example 3:

```Input: nums = [0]
Output: []
```

Constraints:

• `0 <= nums.length <= 3000`
• `-105 <= nums[i] <= 105`

Time: O(n*n)
Space: O(|ans|)

### C++

``````class Solution {
public:
string intToRoman(int num) {
const vector<pair<int, string>> valueSymbols{
{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"},
{90, "XC"},  {50, "L"},   {40, "XL"}, {10, "X"},   {9, "IX"},
{5, "V"},    {4, "IV"},   {1, "I"}};
string ans;

for (const auto& [value, symbol] : valueSymbols) {
if (num == 0)
break;
while (num >= value) {
num -= value;
ans += symbol;
}
}

return ans;
}
};
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
if (nums.size() < 3)
return {};

vector<vector<int>> ans;

sort(begin(nums), end(nums));

for (int i = 0; i + 2 < nums.size(); ++i) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
// choose nums[i] as the first num in the triplet,
// and search the remaining nums in [i + 1, n - 1]
int l = i + 1;
int r = nums.size() - 1;
while (l < r) {
const int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
ans.push_back({nums[i], nums[l++], nums[r--]});
while (l < r && nums[l] == nums[l - 1])
++l;
while (l < r && nums[r] == nums[r + 1])
--r;
} else if (sum < 0) {
++l;
} else {
--r;
}
}
}

return ans;
}
};
class Solution {
public:
string intToRoman(int num) {
const vector<pair<int, string>> valueSymbols{
{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"},
{90, "XC"},  {50, "L"},   {40, "XL"}, {10, "X"},   {9, "IX"},
{5, "V"},    {4, "IV"},   {1, "I"}};
string ans;

for (const auto& [value, symbol] : valueSymbols) {
if (num == 0)
break;
while (num >= value) {
num -= value;
ans += symbol;
}
}

return ans;
}
};
``````

### Java

``````class Solution {
public String intToRoman(int num) {
final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
final String[] symbols = {"M",  "CM", "D",  "CD", "C",  "XC", "L",
"XL", "X",  "IX", "V",  "IV", "I"};
StringBuilder sb = new StringBuilder();

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums.length < 3)
return new ArrayList<>();

List<List<Integer>> ans = new ArrayList<>();

Arrays.sort(nums);

for (int i = 0; i + 2 < nums.length; ++i) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
// choose nums[i] as the first num in the triplet,
// and search the remaining nums in [i + 1, n - 1]
int l = i + 1;
int r = nums.length - 1;
while (l < r) {
final int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
while (l < r && nums[l] == nums[l - 1])
++l;
while (l < r && nums[r] == nums[r + 1])
--r;
} else if (sum < 0) {
++l;
} else {
--r;
}
}
}

return ans;
}
}

``````

### Python

``````class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3:
return []

ans = []

nums.sort()

for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i - 1]:
continue
l = i + 1
r = len(nums) - 1
while l < r:
sum = nums[i] + nums[l] + nums[r]
if sum == 0:
ans.append((nums[i], nums[l], nums[r]))
l += 1
r -= 1
while nums[l] == nums[l - 1] and l < r:
l += 1
while nums[r] == nums[r + 1] and l < r:
r -= 1
elif sum < 0:
l += 1
else:
r -= 1

return ans

``````

#### 3Sum LeetCode Tutorial

Checkout more Solutions here