训练营第七天 leetcode 454 383 18 15

本文介绍了四个编程问题的解决方案:使用哈希表优化的四数之和查找、字符串字母构造判断、数组排序后的三数和查找以及去重优化的四数之和计数。重点在于多指针和去重技巧的应用。
摘要由CSDN通过智能技术生成

454:

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int a : nums1){
            for(int b : nums2){
                int temp = a + b;
                map.put(temp, map.getOrDefault(temp, 0) + 1);

            }
        }
        int res = 0;
        for(int c : nums3){
            for(int d : nums4){
                int temp = c + d;
                res += map.getOrDefault(-c-d, 0);
            }
        }
        return res;
    }
}

383:

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] alphabet = new int[26];
        if(ransomNote.length() > magazine.length()) return false;

        for(int i = 0; i < magazine.length(); i++){
            // map.put(magazine.charAt(i), 1 + getOrDefault(magazine.charAt(i), 0));
            alphabet[magazine.charAt(i) - 'a'] += 1;
        }
        for(int i = 0; i < ransomNote.length(); i++){
            alphabet[ransomNote.charAt(i) - 'a'] -= 1;
            if(alphabet[ransomNote.charAt(i) - 'a'] < 0){
                return false;
            }
        }
        return true;
    }
}

15:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        // int a = 0, b = a + 1, c = nums.length - 1;
        for(int a = 0; a < nums.length; a++){
            if(nums[a] > 0) return res;
            if(a > 0 && nums[a] == nums[a - 1]) return res;
            int b = a + 1;
            int c = nums.length - 1;
            while(b < c){
                if(nums[a] + nums[b] + nums[c] < 0){
                    b++;
                }else if(nums[a] + nums[b] + nums[c] > 0){
                    c--;
                }else{
                    List<Integer> temp = new ArrayList<>();
                    temp.add(nums[a]);
                    temp.add(nums[b]);
                    temp.add(nums[c]);
                    res.add(temp);
                    while(b < c && nums[c] == nums[c - 1]) c--;
                    while(b < c && nums[b] == nums[b + 1]) b++;
                    b++;
                    c--;
                }
            }
        }
        return res;
    }
}

18:

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++) {
            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 (j > i + 1 && nums[j - 1] == nums[j]) { 
                    continue;
                }
                int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
                    int sum = 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;
    }
}

最折磨的一集,说是哈希表但是大部分时间在捣鼓多指针,去重折磨了我好久

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值