算法训练Day7|哈希表

42 阅读2分钟

454 四数相加Ⅱ

public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
    Map<Integer, Integer> countAB = new HashMap<Integer, Integer>();
    for (int u : nums1) {
        for (int v : nums2) {
            countAB.put(u + v, countAB.getOrDefault((u + v), 0) + 1);
        }
    }

    int answer = 0;

    for (int u : nums3) {
        for (int v : nums4){
            if (countAB.containsKey(-u-v)) {
                answer += countAB.get(-u-v);
            }
        }
    }

    return answer;
}

将四组数分为两组, 二重循环遍历其中一组,将所有可能结果的出现次数放在hash表中 再遍历另一组,在另一组中找到与和相反的值的出现次数

383 赎金信

我的思路: 将magazine中的字符统计存放在hash表中 遍历ransomNote,将所有出现的字符去hashmap中-1 最后遍历map,如果map中存在负数则代表无法组成

public boolean canConstruct(String ransomNote, String magazine) {
    if (ransomNote.length() > magazine.length() || ransomNote.length() <= 0) {
        return false;
    }
    Map<Character, Integer> map = new HashMap<>();
    for (int i = 0; i < magazine.length(); i++) {
        map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) + 1);
    }

    for (int i = 0; i < ransomNote.length(); i++) {
        map.put(ransomNote.charAt(i), map.getOrDefault(ransomNote.charAt(i), 0) - 1);
    }

    for (Map.Entry<Character, Integer> tMap : map.entrySet()) {
        if (tMap.getValue() < 0) {
            return false;
        }
    }
    return true;
}

看了题解,发现思路相同,但是使用的自定义数组,快很多 ``` // 定义一个哈希映射数组 int[] record = new int[26];

    // 遍历
    for(char c : magazine.toCharArray()){
        record[c - 'a'] += 1;
    }

    for(char c : ransomNote.toCharArray()){
        record[c - 'a'] -= 1;
    }
    
    // 如果数组中存在负数,说明ransomNote字符串总存在magazine中没有的字符
    for(int i : record){
        if(i < 0){
            return false;
        }
    }

    return true;
    

15 三数之和

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> result = new ArrayList<>();
    //排序
    Arrays.sort(nums);

    for (int i = 0; i < nums.length; i++) {
        if (nums[i] > 0) {
            //如果没有负数,则肯定无解
            return result;
        }

        if (i > 0 && nums[i] == nums[i - 1]) {
            //如果和前一个数字相同,则跳过继续移动
            continue;
        }

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

18 四数之和 双指针经典题,要反复刷

public List<List<Integer>> fourSum(int[] nums, int target) {
    List<List<Integer>> quadruplets = new ArrayList<List<Integer>>();
    if (nums == null || nums.length < 4) {
        return quadruplets;
    }

    //先排序
    Arrays.sort(nums);
    int length = nums.length;
    for ( int i = 0; i<length -3; i++){

        //如果当前数字和前一个相等,则可能是重复解,跳到下一次循环
        if (i > 0 && nums[i] == nums[i - 1]) {
            continue;
        }

        //如果当前最小的四个数字之和大于target,那么之后必定无解,跳出循环
        if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
            break;
        }

        //如果当前数字加上最大的三个数,还小于target,那么当前循环无解,跳到下一次循环
        if ((long) nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
            continue;
        }

        for (int j = i + 1; j < length - 2; j++) {
            //如果当前数字和前一个相等,则可能是重复解,跳到下一次循环
            if (j > i + 1 && nums[j] == nums[j - 1]) {
                continue;
            }
            if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
                break;
            }
            if ((long) nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target) {
                continue;
            }
            int left = j + 1;
            int right = length - 1;
            while (left < right) {
                long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                if (sum == target) {
                    quadruplets.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                    //减少重复解
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    left++;
                    //减少重复解
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    right--;
                } else if (sum < target) {
                    left++;
                } else {
                    right--;
                }

            }
        }
    }
    return quadruplets;
}