【LeetCode - 1】两数之和、三数之和、四数之和、N数之和

两数之和_返回两数的下标

题目
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
思路
方法一:可以用暴力法进行破解,用双层循环遍历nums。一层遍历第一个加数
代码

int[] twoSum1(int[] nums,int target){
        //穷举这两个数的可能
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] == target - nums[i]){
                    return new int[]{i,j};
                }
            }
        }
        //不存在这两个数
        return new int[]{-1,-1};
    }

方法二:可以借助HashMap,将nums加入到HashMap表中,其中nums中的数作为key,索引作为value, 判断Hashmap中存在target-x且这个数的索引值不等于x自身即可。

    //空间换时间,通过一个哈希表记录元素值直到索引的映射,减少时间复杂度
    int[] twoSum2(int[] nums,int target){
        //构造一个哈希表,元素映射到相应的索引
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i],i);
        }
        for (int i = 0;i < nums.length;i++){
            int other = target - nums[i];
            //如果other存在且不是nums[i]本身
            if (map.containsKey(other) && map.get(other) != i){
                return new int[]{map.get(other),i};
            }
        }
        return new int[]{-1,-1};
    }

两数之和,返回两数

题目:输入一个数组nums和一个目标和target,请返回nums中能够凑出target的两个元素,比如输入nums=[1,3,5,6],target = 9,那么算法返回两个元素[3,6],假设只有一对可以凑出target

思路:
先对nums进行排序,然后利用左右指针,从两端相向而行

 public List<Integer> twoSumTarget(int[] nums,int target) {
        //nums数组必须有序
        Arrays.sort(nums);
        List<Integer> res = new ArrayList<>();
        /* 双指针 */
        int low = 0, high = nums.length - 1;
        /* 不能重复取元素,所以是小于号 */
        while (low < high) {
            int sum = nums[low] + nums[high];
            if (sum == target) {
              return {nums[low],nums[high]};

            } else if (sum < target) {
               low++;

            } else {
                high--;
            }
        }
        return {};
    }

两数之和,多对元素和为target

针对上题,nums中可能有多对元素之和等于target,请返回所有和为target的元素对,其中不能出现重复

比如,输入nums = {1,3,1,2,2,3},target = 4
那么算法返回的结果就是[[1,3],[2,2]]

思路:
比上面题目的不同时,关键难点在有多个和为target的数对,不能重复,需要保证每个答案只被添加一次,重复的结果都会被跳过,则还是排序加双指针

  public List<List<Integer>> twoSumTarget(int[] nums,int target) {
        //nums数组必须有序
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        /* 双指针 */
        int low = 0, high = nums.length - 1;
        /* 不能重复取元素,所以是小于号 */
        while (low < high) {
            int sum = nums[low] + nums[high];
            /* 记录索引low和high最初对应的值,方便跳过重复元素 */
            int left = nums[low], right = nums[high];

            if (sum == target) {
                /* 把二元组{left, right}添加到res中 */
                List<Integer> tmp = new LinkedList<>();
                tmp.add(left);
                tmp.add(right);
                res.add(tmp);
                /* 两边跳过重复元素 */
                while (low < high && nums[low] == left) low++;
                while (low < high && nums[high] == right) high--;

            } else if (sum < target) {
                /* 左指针跳过重复元素 */
                while (low < high && nums[low] == left) low++;

            } else {
                /* 右指针跳过重复元素 */
                while (low < high && nums[high] == right) high--;
            }
        }
        return res;
    }

三数之和

题目
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
思路
方法一:穷举法,先确定第一个数字,剩下的两个数字,其实就是和为target - nums[i] 的两个数字,则利用上面方法的两数之和解决

/**
     * 从nums[start]开始,计算有序数组
     * nums中所有和为target的二元组
     * @return
     */
    public List<List<Integer>> threeSum_1(int[] nums) {
        return threeSumTarget(nums,0);
    }
    public List<List<Integer>> twoSumTarget(int[] nums, int start, int target){
        //左指针改为start开始,其他不变
        int lo = start,hi = nums.length - 1;
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        while (lo < hi){
            int sum = nums[lo] + nums[hi];
            int left = nums[lo],right = nums[hi];
            if (sum < target){
                while (lo < hi && nums[lo] == left)
                    lo++;
            }
            else if (sum > target){
                while (lo < hi && nums[hi] == right)
                    hi--;
            }
            else {
                List<Integer> tmp = new LinkedList<>();
                tmp.add(left);
                tmp.add(right);
                res.add(tmp);
                while (lo < hi && nums[lo] == left) lo++;
                while (lo < hi && nums[hi] == right) hi--;
            }
        }
        return res;
    }
    public List<List<Integer>> threeSumTarget(int[] nums,int target){
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        //穷举threeSum 的第一个数
        for (int i = 0;i < nums.length;i++){
            List<List<Integer>> tuples = twoSumTarget(nums,i + 1,target - nums[i]);
            //如果存在满足条件的二元组,再加上nums[i]就是结果三元组
            for (List<Integer> tuple : tuples) {
                tuple.add(nums[i]);
                res.add(tuple);
            }
            //跳过第一个数字重复的情况,否则会出现重复结果
            while (i < nums.length - 1 && nums[i] == nums[i + 1])
                i++;
        }
        return res;
    }

方法二:
跟两数之和不同的是,三数之后要输出的是不同三元组的集合。因此,我们考虑先将nums进行排序,将nums[i]作为第一个加数,从i+1到nums.length-1之间初始化两个指针left,right,为了避免有重复的情况,当nums[i]==nums[i-1],说明有重复的情况,开始下一个循环。如果num[i]+num[left]+num[right]>0,说明加多了,让right–,如果num[i]+num[left]+num[right]<0,说明加少了,让left++,如果等于0,说明符合条件,将这一组解加到集合中,这是也应该避免第二个加数和第三个加数重复的情况。

public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 1; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[left] + nums[right] + nums[i] > 0) {
                    right--;
                } else if (nums[left] + nums[right] + nums[i] < 0) {
                    left++;
                } else {
                    list.add(Arrays.asList(nums[left], nums[right], nums[i]));
                    left++;
                    right--;
                    while (left < right && nums[left] == nums[left - 1]) {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right + 1]) {
                        right--;
                    }
                }

            }
        }
        return list;
    }

四数之和_N数之和

方法一:也可以利用上面的方法,先求出三数之和,再求target- nums[i]的方法,但是该方法就冗余太高

方法二:建立一个nSum的解题框架,

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        /* n为3,从nums[0]开始,计算和为0的三元组 */
        return nSumEqualsTarget(nums, 3, 0, 0);
    }

    /* nSum框架,从nums[start]开始,计算和为target的n元组 */
    private List<List<Integer>> nSumEqualsTarget(int[] nums, int n, int start, int target) {
        int sz = nums.length;
        List<List<Integer>> res = new LinkedList<>();
        /* 至少是2Sum,且数组大小不应该小于n */
        if (n < 2 || sz < n) return res;
        /* 2Sum是base case */
        if (n == 2) {
            /* 双指针 */
            int low = start, high = sz - 1;
            /* 不能重复取元素,所以是小于号 */
            while (low < high) {
                int sum = nums[low] + nums[high];
                /* 记录索引low和high最初对应的值,方便跳过重复元素 */
                int left = nums[low], right = nums[high];

                if (sum == target) {
                    /* 把二元组{left, right}添加到res中 */
                    List<Integer> tmp = new LinkedList<>();
                    tmp.add(left);
                    tmp.add(right);
                    res.add(tmp);
                    /* 两边跳过重复元素 */
                    while (low < high && nums[low] == left) low++;
                    while (low < high && nums[high] == right) high--;

                } else if (sum < target) {
                    /* 左指针跳过重复元素 */
                    while (low < high && nums[low] == left) low++;

                } else if (sum > target) {
                    /* 右指针跳过重复元素 */
                    while (low < high && nums[high] == right) high--;
                }
            }
        } else {
            /* n>2时,递归计算(n-1)Sum的结果 */
            for (int i = start; i < sz; i++) {
                /* 对target-nums[i]计算(n-1)Sum */
                List<List<Integer>> sub = nSumEqualsTarget(nums, n - 1, i + 1, target - nums[i]);
                for (List<Integer> arr : sub) {
                    /* 如果存在满足条件的(n-1)元组,则再加上nums[i]就是nSum */
                    arr.add(nums[i]);
                    res.add(arr);
                }
                /* 跳过重复元素,最终i会停在该重复元素的最后一个 */
                while (i < sz - 1 && nums[i] == nums[i + 1]) i++;
            }
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值