代码随想录算法训练营20期|第七天|哈希表part02|454.四数相加II ● 383. 赎金信 ● 15. 三数之和 ● 18. 四数之和 ● 总结

454.四数相加II

比较巧思的解法,先把nums1 和nums2的数两两相加,并存储sum和次数

再在nums3和nums4里找对应和sum和为0的数值i,j

Time: N^2

Space:N^2, 最坏情况下A和B的值各不相同,相加产生的数字个数为 n^2

java 复制代码
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;

        for (int i : nums1) {
            for (int j : nums2) {
                int sum = i + j;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }

        for (int i : nums3) {
            for (int j : nums4) {
                res += map.getOrDefault(0 - i - j, 0);
            }
        }
        return res;
    }
}
    1. 赎金信

先遍历长的

java 复制代码
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) return false;

        int[] count = new int[26];

        for (char c : magazine.toCharArray()) {
            count[c - 'a']++;
        }


        for (char c : ransomNote.toCharArray()) {
            count[c - 'a']--;
        }


        for (int n : count) {
            if (n < 0) return false;
        }

        return true;

    }
}
    1. 三数之和
java 复制代码
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) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum < 0) {
                    left++;
                } else if (sum > 0) {
                    right--;
                } else {
                    res.add(Arrays.asList(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 res;

    }
}
    1. 四数之和

在三数之和外面再套一层

java 复制代码
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0 && nums[i] > target) return res;
            if (i > 0 && nums[i] == nums[i - 1]) continue;

            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;

                int left = j + 1;
                int right = nums.length - 1;

                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum < target) {
                        left++;
                    } else if (sum > target) {
                        right--;
                    } else {
                        res.add(Arrays.asList(nums[i], nums[j], 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 res;

    }
}
  • 总结
相关推荐
Yang_jie_0311 小时前
笔记:数据结构(栈是否使用底指针以及头指针的初始化值)
数据结构·笔记·算法
lueluelue4714 小时前
LeetCode:滑动窗口
数据结构·算法·leetcode
码少女14 小时前
数据结构——冒泡排序及优化
数据结构·算法·排序算法
青梅橘子皮14 小时前
string---入门OJ题(1)
数据结构·算法
小高Baby@14 小时前
单链表的删操作
数据结构·算法·golang
qq_4542450315 小时前
BasicMethod.Map 设计框架:8 个并行基础模块的数学根基与实现
数据结构·算法·c#
青山木15 小时前
Hot 100 --- 二叉树与递归
java·数据结构·算法·leetcode·深度优先
ysa05103016 小时前
【板子】树上启发式合并
数据结构·c++·笔记·算法
2301_8002561118 小时前
数据结构基础期末复习例题
数据结构·算法
tachibana218 小时前
hot100 将有序数组转换为二叉搜索树(108)
java·数据结构·算法·leetcode