3Sum——LeetCode

2017-08-21  本文已影响3人  远o_O
For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

一、暴力破解:

        for (int i = 0; i < len; ++i)
            for (int j = i + 1; j < len; ++j)
                for (int k = j + 1; k < len; ++k)
    public static List<List<Integer>> fourSum(int[] nums, int target) 
    {
        int len = nums.length;
        if (nums == null || nums.length < 3 )
            return new ArrayList<List<Integer>>();

        List<Integer> ns = null;
        HashSet<List<Integer>> set = new HashSet<>();

        //进行暴力搜索,尝试所有的三人组
        for (int i = 0; i < len; ++i)
        {
            for (int j = i + 1; j < len; ++j)
            {
                for (int k = j + 1; k < len; ++k)
                {
                    //filter the sum not equal target
                    if (nums[i] + nums[j] + nums[k] != target)
                    {
                        continue;
                    }

                    ns = new ArrayList<>();
                    ns.add(nums[i]);
                    ns.add(nums[j]);
                    ns.add(nums[k]);
                    //对三人组进行排序,方便利用HashSet去重
                    Collections.sort(ns);
                    //去重
                    set.add(ns);
                }
            }
        }

        List<List<Integer>> lists = new ArrayList<>();
        for (List<Integer> s : set)
            lists.add(s);

        return lists;
    }

二、排序,固定一个指针,转化为2Sum问题

当 S[i] + S[l] > -S[r] 时,则 r--
当 S[i] + S[l] < -S[r] 时,则 l++
当 S[i] + S[i] = -S[r] 时, 表示是原问题的一个解,则 l++, r--;

    public static List<List<Integer>> threeSumBySort(int[] nums) {
        int len = nums.length;
        List<List<Integer>> lists = new ArrayList<>();
        if (nums == null || nums.length < 3 )
            return lists;

        //sort
        Arrays.sort(nums);
        int low = 0;
        int high = 0;

        //1,2,3,4(固定指针从index = 2 开始)
        for (int i = 0; i < len - 2; ++i)
        {
            //如果最小的数都大于0,break
            if (nums[i] > 0)
                break;

            //优化,去重
            if (i > 0)
            {
                if (nums[i] == nums[i -1])
                    continue;
            }
            low = i + 1;
            high = len - 1;
            while (low < high)
            {
                //优化,去重
                if (low > i + 1)
                {
                    if (nums[low] == nums[low - 1])
                    {
                        ++low;
                        continue;
                    }
                }
                //优化,去重
                if (high < len - 1)
                {
                    if (nums[high] == nums[high + 1])
                    {
                        --high;
                        continue;
                    }
                }

                if (nums[i] + nums[low] + nums[high] == 0)
                {
                    lists.add(Arrays.asList(nums[i], nums[low], nums[high]));
                    ++low;
                    --high;
                }else if (nums[i] + nums[low] + nums[high] < 0) {
                    //如果小于0,则low index右移
                    ++low;
                }else {
                    //如果大于0,则high index左移
                    --high;
                }
            }
        }
        return lists;
    }

https://github.com/yuanoOo/Algorithm/tree/master/LeetCode

上一篇 下一篇

猜你喜欢

热点阅读