代码随想录算法训练营day07_ 454.四数相加II 、383. 赎金信、15. 三数之和、18. 四数之和

454.四数相加II

题目链接
思路:
1.因为本题的四个数B组都是独立的,所以不用考虑重复
2.先取A、B数组两个数相加存入map,重复次数加1
3.再遍历C、D数组,寻找有没有0 - C、D的和

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
 HashMap<Integer, Integer> map = new HashMap<>();
        int temp;
        int res = 0;

        for (int i : nums1) {
            for (int j : nums2) {
                temp = i + j;
                map.put(temp, map.getOrDefault(temp, 0) + 1);
            }
        }

        for (int i : nums3) {
            for (int j : nums4) {
                temp = i + j;
                if (map.containsKey(0-temp)) {
                    res += map.get(0-temp);
                }
            }
        }
        return res;
    }
}

383. 赎金信

题目链接
思路:
1.与242.有效的字母异位词(完全匹配)类似,本题只用目标字符串中的字符在另一个字符串都能找到即可
2.使用record数组进行记录,比map更节省空间

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
 int[] arr = new int[26];
        int temp;
        for (int i = 0; i < magazine.length(); i++) {
            temp = magazine.charAt(i) - 'a';
            arr[temp]++;
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            temp = ransomNote.charAt(i) - 'a';

            if(arr[temp] > 0){
                arr[temp]--;
            } else {
                return false;
            }
        }
        return true;
    }
}

15. 三数之和

题目链接

方法一:双指针

思路:
1.因为本题和两数之和不一样,返回的是所有的结果集,而不是下标,所以可以使用双指针
2.先将数组排序,方便去重和剪枝
3.先用一个for循环来确定一个数nums[i],然后再用双指针寻找后两个数
4.i > 0 && nums[i] == nums[i - 1]注意这两个条件的顺序不能交换,因为代码是按顺序从左往右执行,如果交换,会数组下标越界

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            //因为数组已经排序,后面的数一定比这个数大
            if (nums[i] > 0) {
                return res;
            }
            //去重,为什么不使用nums[i] == nums[i + 1],可能会漏掉一种情况
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    // 剪枝
                while (right > left && nums[right] == nums[right - 1]) {
                    right--;
                }
                while (right > left && nums[left] == nums[left + 1]) {
                    left++;
                }
                
                right--;
                left++;
                }
            }
        }
        return res;
    }
}

方法二:模板

思路:
1.其实也是类似于双指针的思路,既然我们做过两数之和,那本题可以看成一个数加另外两个数的和,这样四数之和不管多少数都能用上
2.由于也使用了双指针,所以leetcode上的两数之和是返回下标,不适用

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = nSumTarget(nums, 0, 0, 3);
        return res;
    }

    public List<List<Integer>> nSumTarget(int[] nums, int startIndex, int target, int n) {
        if (nums == null) {
            return new ArrayList<>();
        }
        int len = nums.length;
        if (n < 2 || len < n) {
            return res;
        }
        if (n == 2) {
            int lo = startIndex;
            int hi = len - 1;
            while (lo < hi) {
                int sum = nums[lo] + nums[hi];
                int left = nums[lo];
                int 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 if (sum == target) {
                    ArrayList<Integer> path = new ArrayList<>();
                    path.add(nums[lo]);
                    path.add(nums[hi]);
                    res.add(path);
                    while (lo < hi && nums[lo] == left) {
                        lo++;
                    }
                    while (lo < hi && nums[hi] == right) {
                        hi--;
                    }
                }
            }
        } else {
            for (int i = startIndex; i < len; i++) {
                List<List<Integer>> lists = nSumTarget(nums, i + 1, target - nums[i], n - 1);
                for (List<Integer> list : lists) {
                    list.add(nums[i]);
                    res.add(list);
                }
                while (i < len - 1 && nums[i] == nums[i + 1]) {
                    i++;
                }
            }
        }
        return res;
    }

}

18. 四数之和

题目链接

方法一:双指针

思路:
1.既然三数之和使用一个for循环确定一个数,再使用双指针确定另外两个数,那么四数之和无非是还要在多确定一个数,我们可以多加一个for循环
2.为什么if (nums[i] > 0 && nums[i] > target)if (nums[i] > 0 && nums[i] + nums[j] > target)一定要先加上一个nums[i]>0才行,因为数组的前面可能有负数,会导致循环提前结束,漏掉结果
3.其他代码和三数之和一样
4.自己在第二层for循环也加了一个去重,时间效率有所提高

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
       
        for (int i = 0; i < nums.length; i++) {

            // nums[i] > target 直接返回, 剪枝操作
            if (nums[i] > 0 && nums[i] > target) {
                return result;
            }

            if (i > 0 && nums[i - 1] == nums[i]) {
                continue;
            }
            
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > 0 && nums[i] + nums[j] > target) {
                    continue;
                }
                if (j > i + 1 && nums[j - 1] == nums[j]) {
                    continue;
                }

                int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        
                        while (right > left && nums[right] == nums[right - 1]) right--;
                        while (right > left && nums[left] == nums[left + 1]) left++;

                        left++;
                        right--;
                    }
                }
            }
        }
        return result;
    }
}

方法二:模板

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = nSumTarget(nums, 0, 0, 3);
        return res;
    }

    public List<List<Integer>> nSumTarget(int[] nums, int startIndex, int target, int n) {
        if (nums == null) {
            return new ArrayList<>();
        }
        int len = nums.length;
        if (n < 2 || len < n) {
            return res;
        }
        if (n == 2) {
            int lo = startIndex;
            int hi = len - 1;
            while (lo < hi) {
                int sum = nums[lo] + nums[hi];
                int left = nums[lo];
                int 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 if (sum == target) {
                    ArrayList<Integer> path = new ArrayList<>();
                    path.add(nums[lo]);
                    path.add(nums[hi]);
                    res.add(path);
                    while (lo < hi && nums[lo] == left) {
                        lo++;
                    }
                    while (lo < hi && nums[hi] == right) {
                        hi--;
                    }
                }
            }
        } else {
            for (int i = startIndex; i < len; i++) {
                List<List<Integer>> lists = nSumTarget(nums, i + 1, target - nums[i], n - 1);
                for (List<Integer> list : lists) {
                    list.add(nums[i]);
                    res.add(list);
                }
                while (i < len - 1 && nums[i] == nums[i + 1]) {
                    i++;
                }
            }
        }
        return res;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小C卷Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值