【leetcode】第三章 哈希表part02

454.四数相加II

java 复制代码
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
    HashMap<Integer,Integer> map = new HashMap<>();
    // 统计频率
    for (int i = 0; i < nums1.length; i++) {
        for (int j = 0; j < nums2.length; j++) {
            int num = nums1[i] + nums2[j];
            map.put(num,map.getOrDefault(num,0)+1);
        }
    }
    int cnt = 0;
    for (int i = 0; i < nums3.length; i++) {
        for (int j = 0; j < nums4.length; j++) {
            int restNum = nums3[i] + nums4[j];
            if (map.containsKey(-restNum)) {
                cnt += map.get(-restNum);
            }
        }
    }

    return cnt;
}

383. 赎金信

  • 使用map方法
java 复制代码
public boolean canConstruct(String ransomNote, String magazine) {
    // ransomNote是magazine的子串
    // aa aabcdea
    HashMap<Character,Integer> map = new HashMap<>();
    for (char c : ransomNote.toCharArray()) {
        map.put(c,map.getOrDefault(c,0)+1);
    }

    for (int i = 0; i < magazine.length(); i++) {
        char ch = magazine.charAt(i);

        if (map.containsKey(ch)) {
            map.put(ch,map.get(ch)-1);
        }
    }

    // 判断
    for (Integer cnt : map.values()) {
        if (cnt > 0) return false;
    }
    return true;

}
  • 使用数组方法
java 复制代码
public boolean canConstruct(String ransomNote, String magazine) {
    // ransomNote是magazine的子串
    // aa ab
    int[] hash = new int[26];

    for (int i = 0; i < magazine.length(); i++) {
        char ch = magazine.charAt(i);
        hash[ch-'a']++;
    }

    for (int i = 0; i < ransomNote.length(); i++) {
        char c  = ransomNote.charAt(i);
        hash[c-'a']--;
        if (hash[c-'a'] < 0) {
            return false;
        }
    }
    return true;

}

15 三数之和

java 复制代码
public List<List<Integer>> threeSum(int[] nums) {
    Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < nums.length-2; i++) {
            if (nums[i] > 0) break;
            if (i != 0 && nums[i] == nums[i-1]) {
                continue;
            }

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

            while (left < right) {
                int target = nums[i] + nums[left] + nums[right];
                if (target == 0) {
                    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--;
                }
                else if (target < 0) {
                    left++;
                }
                else {
                    right--;
                }
            }

        }
        return res;
}

18. 四数之和

java 复制代码
public List<List<Integer>> fourSum(int[] nums, int target) {
    // 输入:nums = [1,0,-1,0,-2,2], target = 0
    //输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
    List<List<Integer>> res = new ArrayList<>();
    Arrays.sort(nums);

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

        for (int j = i+1; j < nums.length-2; j++) {
            // 剪枝
            if (j > i+1 && nums[j] == nums[j-1]) continue;
            int left = j+1;
            int right = nums.length-1;

            while (left < right) {
                long num = (long)nums[i] + nums[j] + nums[left] + nums[right];
                if (num == target) {
                    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--;
                }
                else if (num < target) {
                    left++;
                }
                else {
                    right--;
                }
            }
        }
    }
    return res;
}
相关推荐
我爱豆子1 小时前
Leetcode Hot 100刷题记录 -Day19(回文链表)
java·算法·leetcode·链表
程序猿练习生1 小时前
C++速通LeetCode中等第18题-删除链表的倒数第N个结点(最简单含注释)
c++·leetcode·链表
m0_571957581 小时前
Java | Leetcode Java题解之第424题替换后的最长重复字符
java·leetcode·题解
我明天再来学Web渗透2 小时前
【hot100-java】【组合总和】
java·开发语言·数据结构·windows·算法·链表·散列表
我要学编程(ಥ_ಥ)8 小时前
双指针算法专题(2)
数据结构·算法·leetcode
我要学编程(ಥ_ಥ)10 小时前
滑动窗口算法专题(1)
java·数据结构·算法·leetcode
LluckyYH10 小时前
代码随想录Day 46|动态规划完结,leetcode题目:647. 回文子串、516.最长回文子序列
数据结构·人工智能·算法·leetcode·动态规划
huanxiangcoco10 小时前
73. 矩阵置零
python·leetcode·矩阵
源代码:趴菜11 小时前
LeetCode118:杨辉三角
算法·leetcode·动态规划
luluvx11 小时前
LeetCode[中等] 74.搜索二维矩阵
算法·leetcode·矩阵