day07|454.四数相加II,383. 赎金信,15. 三数之和,18. 四数之和

454.四数相加II

题目链接:https://leetcode.cn/problems/4sum-ii/

思路

遍历nums1和nums2,将nums1和nums2的结果存储到Map中,key-value对应两数之和-出现次数。遍历nums3和nums4,要求两数之和+Map中的key值=0。时间O(n^2), 空间O(n^2)

代码

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i : nums1) {
            for (int j : nums2) {
                map.put(i + j, map.getOrDefault(i + j, 0) + 1);
            }
        }
        for (int i : nums3) {
            for (int j : nums4) {
                if (map.containsKey(-i - j)) {
                    res += map.get(-i - j);
                }
            }
        }
        return res;
    }
}

383. 赎金信

题目链接:https://leetcode.cn/problems/ransom-note/

思路

该题与242. 有效的字母异位词类似。时间O(m+n),空间O(1)

代码

//Map
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map = new HashMap<>();
        for (Character c : magazine.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (Character c : ransomNote.toCharArray()) {
            if (!map.containsKey(c) || map.get(c) <= 0) {
                return false;
            }
            map.put(c, map.get(c) - 1);
        }
        return true;
    }
}
//数组
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] arr = new int[26];
        for (Character c : magazine.toCharArray()) {
            arr[c - 'a']++;
        }
        for (Character c : ransomNote.toCharArray()) {
            if (arr[c - 'a'] <= 0) {
                return false;
            }
            arr[c - 'a']--;
        }
        return true;
    }
}

15. 三数之和

题目链接:https://leetcode.cn/problems/3sum/

思路

遍历+双指针。时间O(n^2),空间O(log n)

代码

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            //nums[i]去重
            if (i > 0 && nums[i - 1] == nums[i]) {
                continue;
            }
            int left = i + 1, right = nums.length - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum == 0) {
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    //nums[left]去重
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    left++;
                    //nums[right]去重
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    right--;
                } else if (sum > 0) {
                    right--;
                } else {
                    left++;
                }
            }
        }
        return res;
    }
}

18. 四数之和

题目链接:https://leetcode.cn/problems/4sum/

思路

与三数之和类似,双层循环+双指针。时间O(n^3),空间O(log n)

代码

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            //nums[i]去重
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            for (int j = i + 1; j < nums.length - 1; j++) {
                //nums[j]去重
                if (j != i + 1 && nums[j] == nums[j - 1]) {
                    continue;
                }
                int left = j + 1, right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum == target) {
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        //nums[left]去重
                        while (left < right && nums[left] == nums[left + 1]) {
                            left++;
                        }
                        left++;
                        //nums[right]去重
                        while (left < right && nums[right] == nums[right - 1]) {
                            right--;
                        }
                        right--;
                    } else if (sum > target) {
                        right--;
                    } else {
                        left++;
                    }
                }
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值