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

29 阅读2分钟

小结:最近事情比较多,笔记简单记录一下

454.四数相加II

题目链接

解题思路:运用了哈希表中的map,把四个数组分成两两一组

map中存的什么需要思考清楚

解题代码:

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

        for(int c : nums3){
            for(int d : nums4){
                int target = 0 - (c+d);
                if(map.containsKey(target)){
                    res += map.get(target);
                }
            }
        }
        return res;
    }
}

383. 赎金信

题目链接

解题思路:和有效字母异位词思路一样,算是进阶。

解题代码:

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        //如果 ransomNote 的长度大于 magazine,直接返回 false
        if(ransomNote.length() > magazine.length()){
            return false;
        }
        //定义一个长度为26的数组
        int[] nums = new int[26];
        //计算ransomNote中每个字母出现的次数
        for(int i = 0;i < ransomNote.length();i++){
            nums[ransomNote.charAt(i) - 'a']++;
        }
        //减去 magazine 中每个字母出现的次数
        for(int i = 0;i < magazine.length();i++){
            nums[magazine.charAt(i) - 'a']--;
        }
        //遍历数组,如果数组中的元素大于0,说明 ransomNote 中有 magazine 没有的字符
        //如果 小于或者等于0,说明 ransomNote 中的字符 magazine 都有
        for(int a : nums){
            if(a > 0){
                return false;
            }
        }
        return true;
    }
}

15. 三数之和

题目链接

解题思路:双指针的方法更好,但是需要注意剪枝和去重

解题代码

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> resultList = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0;i < nums.length;i++){
            //剪枝
            if(nums[i]>0){
                return resultList;
            }
            //去重
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            int left = i+1;
            int right = nums.length-1;
            while(right>left){
                //和大于0,右指针需要前移
                if(nums[i] + nums[left] + nums[right] > 0){
                    right--;
                }else if(nums[i] + nums[left] + nums[right] < 0){ //和小于0,左指针需要后移
                    left++;
                }else{
                    resultList.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    //左右指针去重
                    while(right > left && nums[right] == nums[right-1]){
                        right--;
                    }
                    while(left < right && nums[left] == nums[left+1]){
                        left++;
                    }
                    right--;
                    left++;
                }
            }
        }
        return resultList;
    }
}

18. 四数之和

题目链接

解题思路:在三数之和的基础上剪枝和去重的逻辑更多更细节,相加的和需要转成long,不然会造成溢出

解题代码:

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for(int k = 0;k < nums.length;k++){
            //一级剪枝
            if(nums[k] > 0 && target > 0 && nums[k] > target){
                return result;
            }
            //一级去重
            if(k > 0 && nums[k] == nums[k-1]){
                continue;
            }
            for(int i = k + 1;i < nums.length;i++){
                //二级剪枝
                if(nums[k] + nums[i] > target && nums[k] + nums[i] > 0 && target >0){
                    break;
                }
                //二级去重
                if(i > k + 1 && nums[i] == nums[i-1]){
                    continue;
                }
                int left = i + 1;
                int right = nums.length - 1;
                while(left < right){
                    //将和转成long,不然会溢出
                    long sum = (long)nums[k]+nums[i]+nums[left]+nums[right];
                    if(sum > target){
                        right--;
                    }else if(sum < target){
                        left++;
                    }else{
                        result.add(Arrays.asList(nums[k],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--;
                    }
                }
            }
        }
        return result;
    }
}