Day7 哈希表part02

今日任务

  • 454.四数相加II
  • 383. 赎金信
  • 15. 三数之和
  • 18. 四数之和
  • 总结

详细布置

454.四数相加II

建议:本题是使用map 巧妙解决的问题,好好体会一下 哈希法 如何提高程序执行效率,降低时间复杂度,当然使用哈希法会提高空间复杂度,但一般来说我们都是舍空间 换时间, 工业开发也是这样。

题目链接/文章讲解/视频讲解:代码随想录

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map hashmap = new HashMap<>();
        for (int i = 0; i < nums1.length; i++){
            for (int j = 0; j < nums2.length; j++){
                int sum = nums1[i] + nums2[j];
                hashmap.put(sum,hashmap.getOrDefault(sum, 0)+1);//value统计出现的次数
            }
        }
        int count = 0;
        for(int num0 : nums3){
            for(int num1 : nums4){
                int target = -(num0+num1);
                count 

你可能感兴趣的:(散列表,数据结构)