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

58 阅读1分钟

454.四数相加II

题目链接:454.四数相加II

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        // <i+j, 和出现的次数>
        for (Integer i : nums1) {
            for (Integer j : nums2) {
                map.put(i + j, map.get(i +  j) != null ? map.get(i +  j) + 1 : 1);
            }
        }

        for (Integer i : nums3) {
            for (Integer j : nums4) {
                if (map.containsKey(-(i + j))) {
                    count += map.get(-(i + j));
                }
            }
        }

        return count;
    }
}

383.赎金信

题目链接:383. 赎金信

思路:

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (char i : magazine.toCharArray()) {
            map.put(i, map.get(i) == null ? 1 : map.get(i) + 1);
        }

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

15.三数之和

题目链接:15.三数之和

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) {
                return res;
            }
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[i] + nums[left] + nums[right] > 0) right--;
                else if (nums[i] + nums[left] + nums[right] < 0) left++;
                else {
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    
                    right--; 
                    left++;
                }
            }
        }

        return res;
    }
}

8.四数之和

题目链接:8.四数之和

思路:

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++) {

            // nums[i] > target 直接返回, 剪枝操作
            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) {
                    long sum = (long) 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;
    }
}