【算法刷题day7】Leetcode:454. 四数相加 II、383. 赎金信、15. 三数之和、18. 四数之和

草稿图网站
java的Deque

Leetcode 454. 四数相加 II

题目:454. 四数相加 II
解析:代码随想录解析

解题思路

使用HashMap来记录nums1+nums2的所有情况,寻找-(nums3+nums4)的次数。

代码

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int numI : nums1)
            for(int numJ : nums2){
                int sum = numI + numJ;
                map.put(sum, map.getOrDefault(sum, 0) + 1);//getOrDefault如果找到返回sum,否则返回0
            }
        int res = 0;
        for (int numI : nums3)
            for (int numJ : nums4){
                int sum = numI + numJ;
                res += map.getOrDefault(-sum, 0);
            }
        return res;
    }
}

总结

暂无

Leetcode 383. 赎金信

题目:383. 赎金信
解析:代码随想录解析

解题思路

HashMap来记录和删除。

代码

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++){
            int c = magazine.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++){
            int c = ransomNote.charAt(i);
            int count = map.getOrDefault(c, 0);
            if (count == 0)
                return false;
            map.put(c, count - 1);
        }
        return true;
    }
}

总结

这题可以用Hash数组来解决

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] hash = new int[26];
        for (char c : magazine.toCharArray())
            hash[c - 'a']++;
        for (char c : ransomNote.toCharArray()){
            if (hash[c - 'a'] == 0)
                return false;
            hash[c - 'a']--;
        }
        return true;
    }
}

Leetcode 15. 三数之和

题目:15. 三数之和
解析:代码随想录解析

解题思路

自己没想出来,参考了Carl的代码。
遍历数组,设定最小的i,然后使用双指针寻找到所有可能的结果。

代码

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)
                break;
            // if (i > 0 && nums[i] == nums[i-1])
            //    continue;
            int left = i + 1;
            int 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{
                    res.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--;
                }
            }
            while (i < nums.length - 1 && nums[i] == nums[i+1])
               i++;
        }
        return res;
    }
}

总结

暂无

Leetcode 18. 四数之和

题目:18. 四数之和
解析:代码随想录解析

解题思路

两层循环后,双指针遍历。
continue条件要想到元素大于target的同时,要大于等于0才能break

代码

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++){
            if (nums[i] > target && nums[i] >= 0)
                break;
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            for (int j = i + 1; j < nums.length; j++){
                if (nums[i] + nums[j] > target && nums[i] + nums[j] >= 0)
                    break;
                if (j > i + 1 && nums[j] == nums[j - 1])
                    continue;
                int left = j + 1;
                int right = nums.length - 1;
                while (left < right){
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target)
                        right--;
                    else if (sum < target)
                        left++;
                    else{
                        res.add(Arrays.asList(nums[i], nums[j], 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 res;
    }
}

总结

要想到和三数之和是一个道理

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Allmight_Q

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

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

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

打赏作者

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

抵扣说明:

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

余额充值