15.三数之和

╰半橙微兮° 2021-09-17 02:34 420阅读 0赞

题目描述

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

  1. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
  2. 满足要求的三元组集合为:
  3. [
  4. [-1, 0, 1],
  5. [-1, -1, 2]
  6. ]

最首先想到的是来三层循坏,然后将和为0的三个变量加入集合即可。
代码如下:

  1. public static List<List<Integer>> threeSum(int[] nums) {
  2. List<List<Integer>> allList = new ArrayList<List<Integer>>();
  3. for(int i = 0;i < nums.length;i++){
  4. for(int j = 0;j < nums.length;j++){
  5. for(int k = 0;k < nums.length;k++){
  6. if(i != j && i != k && j !=k && nums[i] + nums[j] + nums[k] == 0){
  7. List<Integer> list = new ArrayList<Integer>();
  8. list.add(nums[i]);
  9. list.add(nums[j]);
  10. list.add(nums[k]);
  11. Collections.sort(list);
  12. //判断重复
  13. if(!allList.contains(list)) {
  14. allList.add(list);
  15. }
  16. }
  17. }
  18. }
  19. }
  20. return allList;
  21. }

通过上面的代码我们可知,该方法虽然很简单明了,但是时间复杂度却太高了,提交后会超时的。
由a+b+c = 0得,我们只需要确定两个变量a、b,然后再寻找是否存在c即可,这样我们就只需要两层循坏了。在寻找c的过程中,我们可以使用2分法,所有需要对数组进行排序。如果排序后数组中最小的那个数大于0或者最大的那个数小于0、或者数组中最多有2个元素,那么直接返回空的集合即可。

  1. public static List<List<Integer>> threeSum2(int[] nums){
  2. List<List<Integer>> allList = new ArrayList<List<Integer>>();
  3. Arrays.sort(nums);
  4. if(nums.length <3 || nums[0] > 0 || nums[nums.length-1] <0)
  5. return allList;
  6. int len = nums.length;
  7. for(int i = 0;i < len;i++){
  8. //当第一个变量的值相同时,后面的判断任然和上次相同,所以继续判断没有意义
  9. if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
  10. for (int j = i + 1; j < len; j++) {
  11. int k = 0 - nums[i] - nums[j];
  12. if (Arrays.binarySearch(nums, j + 1, len, k) >= 0) {
  13. List<Integer> list = new ArrayList<Integer>();
  14. list.add(nums[i]);
  15. list.add(nums[j]);
  16. //避免使用Collections.sort(list);
  17. if (k >= nums[j]) {
  18. list.add(k);
  19. } else {
  20. list.add(1, k);
  21. }
  22. //判断是否重复
  23. if(!allList.contains(list)) {
  24. allList.add(list);
  25. }
  26. }
  27. }
  28. }
  29. }
  30. return allList;
  31. }

上面的代码较第一段的时间复杂度有所下降,但是当确定1个变量的值后,还需要遍历后面的值来确定第2个变量,以此来确定第三个变量的值,这样还是会超时的,仍然不能够满足我们的需求。
因为a + b + c = 0,targe = 0 - a;所以 b + c = targe。我们可以用两个指针指向数组剩余部分的首尾,开始遍历,这样就比从头遍历到尾的时间更低,代码如下:

  1. public static List<List<Integer>> threeSum(int[] nums) {
  2. List<List<Integer>> allList = new ArrayList<>();
  3. int len = nums.length;
  4. Arrays.sort(nums);
  5. for(int i = 0;i < nums.length;i++){
  6. //如果三个数字中最小的那个数子大于0,则没必要进行后面的步骤
  7. if(nums[i] > 0)
  8. break;
  9. if(i==0 || (i > 0 && nums[i] != nums[i-1])){
  10. int j = i+1;
  11. int k = len - 1;
  12. while (j < k)
  13. {
  14. if(nums[j] + nums[k] == -nums[i] ){
  15. List<Integer> list = new ArrayList<>();
  16. list.add(nums[i]);
  17. list.add(nums[j]);
  18. list.add(nums[k]);
  19. //跳过重复的元素
  20. while (j < k && nums[j] == nums[j + 1]) ++j;
  21. //跳过重复的元素
  22. while (j < k && nums[k] == nums[k - 1]) --k;
  23. ++j; --k;
  24. }else if(nums[j] + nums[k] < -nums[i]){
  25. ++j;
  26. }else{
  27. --k;
  28. }
  29. }
  30. }
  31. }
  32. return allList;
  33. }

通过上面的代码,我们也不需要进行集合是否重复,因为所有重复的集合都已经被我们过滤了。

发表评论

表情:
评论列表 (有 0 条评论,420人围观)

还没有评论,来说两句吧...

相关阅读

    相关 15.之和

    [题目][Link 1] 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足

    相关 15. 之和

    链接:https://leetcode-cn.com/problems/3sum 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,

    相关 15.之和

    题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元