代码随想录训练营第七天|454 四数相加、383 赎金信、15 三数之和、18 四数之和

454 四数相加

另一道两数之和的题目

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

        int count = 0;
        for(int num3 : nums3) {
            for(int num4 : nums4) {
                if (map.containsKey(0 - num3 - num4)) {
                    count += map.get(0 - num3 - num4);
                }
            }
        }
        return count;
    }
}

383 赎金信

很简单的hash数据结构应用

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : magazine.toCharArray()) {
            int count = map.getOrDefault(c, 0) + 1;
            map.put(c, count);
        }

        for(char c : ransomNote.toCharArray()) {
            if (map.containsKey(c)) {
                int count = map.get(c) - 1;
                if (count == 0) {
                    map.remove(c);
                } else {
                    map.put(c, count);
                }
            } else {
                return false;
            }
        }

        return true;
    }
}

15 三数之和

使用的是双指针法,这个题目看labuladong的更易看懂:labuladong一个方法团灭 nSum 问题

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        return threeSum(nums, 0);
    }

    public List<List<Integer>> threeSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for(int i = 0 ; i < nums.length; i++) {
            List<List<Integer>> tuples = twoSum(nums, i + 1, target - nums[i]);
            for (List<Integer> tuple : tuples) {
                tuple.add(nums[i]);
                res.add(tuple);
            }
            while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
        }
        return res;
    }

    public List<List<Integer>> twoSum(int[] nums, int start, int target) {
        List<List<Integer>> res = new ArrayList<>();
        int leftIndex = start, rightIndex = nums.length - 1;
        while(leftIndex < rightIndex) {
            int sum = nums[leftIndex] + nums[rightIndex];
            int left = nums[leftIndex], right = nums[rightIndex];
            if (sum < target) {
                while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
            } else if (sum > target) {
                while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
            } else {
            	// 注意此处不能使用Arrays.asList(),不然会导致UnsupportedOperationException
                List<Integer> tuple = new ArrayList<>();
                tuple.add(nums[leftIndex]);
                tuple.add(nums[rightIndex]);
                res.add(tuple);
                while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
                while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
            }
        }
        return res;
    }
}

UnsupportedOperationException异常的解决方法

18 四数之和

注意target范围,换成long类型

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++) {
            List<List<Integer>> triples = threeSum(nums, i + 1, target - nums[i]);
            for (List<Integer> triple : triples) {
                triple.add(nums[i]);
                res.add(triple);
            }
            while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
        }
        return res;
    }

    public List<List<Integer>> threeSum(int[] nums, int start, long target) {
        List<List<Integer>> res = new ArrayList<>();
        for(int i = start ; i < nums.length; i++) {
            List<List<Integer>> tuples = twoSum(nums, i + 1, target - nums[i]);
            for (List<Integer> tuple : tuples) {
                tuple.add(nums[i]);
                res.add(tuple);
            }
            while(i < nums.length - 1 && nums[i] == nums[i + 1]) i++;
        }
        return res;
    }

    public List<List<Integer>> twoSum(int[] nums, int start, long target) {
        List<List<Integer>> res = new ArrayList<>();
        int leftIndex = start, rightIndex = nums.length - 1;
        while(leftIndex < rightIndex) {
            int sum = nums[leftIndex] + nums[rightIndex];
            int left = nums[leftIndex], right = nums[rightIndex];
            if (sum < target) {
                while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
            } else if (sum > target) {
                while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
            } else {
                List<Integer> tuple = new ArrayList<>();
                tuple.add(nums[leftIndex]);
                tuple.add(nums[rightIndex]);
                res.add(tuple);
                while(leftIndex < rightIndex && nums[leftIndex] == left) leftIndex++;
                while(leftIndex < rightIndex && nums[rightIndex] == right) rightIndex--;
            }
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值