Leetcode刷题Java15. 三数之和

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

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

示例:

给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/3sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

class Solution {
        public List<List<Integer>> threeSum(int[] nums) {
            //转换成熟悉的题目,两数之和
            //a+b=-c(target)
            if (nums == null || nums.length < 3) return Collections.emptyList();
//            return threeSumI(nums);
//            return threeSumII(nums);

            return threeSumIII(nums);
        }

        //方法三:双指针法
        //先排序,定义3个指针中最小数字的指针i,枚举i;
        //left,right分别位于(i,len(nums))两端
        //s = nums[i] + nums[left] + nums[right]
        //如果s > 0,right--;s < 0,left++;s=0,记录i,left,right
        private List<List<Integer>> threeSumIII(int[] nums) {
            //先排序,便于去重
            Arrays.sort(nums);
            List<List<Integer>> result = new LinkedList<>();
            //定义i为三个数中最小数字的指针,枚举i
            for (int i = 0; i < nums.length - 2; i++) {
                //如果最小数num[i] > 0,三数之和不可能为0,跳过
                if (nums[i] > 0) break;
                //跳过重复值
                if (i > 0 && nums[i] == nums[i - 1]) continue;
                int left = i + 1, right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[left] + nums[right];
                    if (sum < 0) {
                        left++;
                    } else if (sum > 0) {
                        right--;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                        //去重
                        while (left < right && nums[left] == nums[left + 1]) left++;
                        while (left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }
                }
            }
            return result;
        }

        //方法二:利用Hash表缓存结果,二重循环O(n^2)
        private List<List<Integer>> threeSumII(int[] nums) {
            Set<List<Integer>> result = new LinkedHashSet<>();
            for (int i = 0; i < nums.length - 1; i++) {
                Map<Integer, Integer> map = new HashMap<>();
                for (int j = i + 1; j < nums.length; j++) {
                    int target = -nums[i] - nums[j];
                    if (map.containsKey(target)) {
                        List<Integer> list = Arrays.asList(nums[i], nums[j], target);
                        list.sort(Comparator.naturalOrder());
                        result.add(list);
                    } else {
                        map.put(nums[j], nums[j]);
                    }
                }
            }
            return new ArrayList<>(result);
        }

        //方法一:暴力求解,三重循环O(n^3)
        private List<List<Integer>> threeSumI(int[] nums) {
            Set<List<Integer>> result = new LinkedHashSet<>();
            //先排序,方便去重
            Arrays.sort(nums);
            for (int i = 0; i < nums.length - 2; i++) {
                for (int j = i + 1; j < nums.length - 1; j++) {
                    for (int k = j + 1; k < nums.length; k++) {
                        if (nums[i] + nums[j] + nums[k] == 0) {
                            List<Integer> list = Arrays.asList(nums[i], nums[j], nums[k]);
                            result.add(list);
                        }
                    }
                }
            }
            return new ArrayList<>(result);
        }
    }
发布了19 篇原创文章 · 获赞 2 · 访问量 1448

猜你喜欢

转载自blog.csdn.net/Bonbon_wen/article/details/105453099