给你一个包含 n 个整数的数组 nums ,判断 nums 中是否存在三个元素 a, b, c, 使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例1:
输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
示例2:
输入:nums = []
输出:[]
示例3:
输入:nums = [0]
输出:[]
暴力解法
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
int length = nums.length;
List<List<Integer>> resultList = new ArrayList<>();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
for (int k = j + 1; k < length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
resultList.add(Arrays.asList(nums[i], nums[j], nums[k]));
}
}
}
}
return resultList;
}
上述暴力思路有没有问题呢?
假设 int[] nums = {-1,0,1,2,-1,4};
如果按照上述算法,结果是:[[-1, 0, 1], [-1, 2, -1], [0, 1, -1]]
而 [-1, 0, 1] 和 [0, 1, -1] 实际是重复的三元组,那如何去重呢?
如果 nums[i] + nums[j] + nums[k] == 0,我们可以先对 nums[i],nums[j],nums[k] 三个元素排个序,然后拼接为字符串,利用字符串去重
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
int length = nums.length;
List<List<Integer>> resultList = new ArrayList<>();
Set<String> tempStrSet = new HashSet<>();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
for (int k = j + 1; k < length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
List<Integer> tempList = Arrays.asList(nums[i], nums[j], nums[k]);
Collections.sort(tempList);
String tempStr = tempList.toString();
if (!tempStrSet.contains(tempStr)) {
tempStrSet.add(tempStr);
resultList.add(tempList);
}
}
}
}
}
return resultList;
}
优化上述算法,移除 tempStr 临时变量的使用
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
int length = nums.length;
Set<List<Integer>> resultList = new HashSet<>();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
for (int k = j + 1; k < length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
List<Integer> tempList = Arrays.asList(nums[i], nums[j], nums[k]);
Collections.sort(tempList);
resultList.add(tempList);
}
}
}
}
return new ArrayList<>(resultList);
}
优化上述算法,先对数组进行排序,以减少对结果的排序次数
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
Arrays.sort(nums);
int length = nums.length;
Set<List<Integer>> resultList = new HashSet<>();
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
for (int k = j + 1; k < length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
resultList.add(Arrays.asList(nums[i], nums[j], nums[k]));
}
}
}
}
return new ArrayList<>(resultList);
}
优化上述算法:在上述算法中,已经先对数组做了排序,那么对于有序数组的元素查找可以考虑使用二分查找,二分查找的时间复杂度会比线性查找好很多
可以先固定一个元素,然后再剩下的有序数组中通过二分查找,找到两个元素的和等于被指定元素的相反值
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
Arrays.sort(nums);
int length = nums.length;
Set<List<Integer>> resultList = new HashSet<>();
for (int i = 0; i < length - 2; i++) {
int target = - nums[i];
int left = i + 1;
int right = length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++;
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
return new ArrayList<>(resultList);
}
假设 nums = [-1,0,1,2,-1,-4,1,1,0]
排序后 nums = [-4,-1,-1,0,0,1,1,1,2]
当外层循环到第二个元素时,left = 3 right = 7 和 left = 4 right = 6 实际是重复结果
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
Arrays.sort(nums);
int length = nums.length;
Set<List<Integer>> resultList = new HashSet<>();
for (int i = 0; i < length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int target = -nums[i];
int left = i + 1;
int right = length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[++left]) {}
while (left < right && nums[right] == nums[--right]) {}
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
return new ArrayList<>(resultList);
}
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) {
return new ArrayList<>();
}
Arrays.sort(nums);
int length = nums.length;
List<List<Integer>> resultList = new ArrayList<>();
for (int i = 0; i < length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int target = -nums[i];
int left = i + 1;
int right = length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
resultList.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (left < right && nums[left] == nums[++left]) {}
while (left < right && nums[right] == nums[--right]) {}
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
return resultList;
}