DAY07——哈希表part2

文章提供了四个Java类Solution,分别实现了寻找数组中四数之和为0的组合、判断能否用杂志中的字符构造赎金信、找出数组中三数之和为0的组合以及寻找数组中四数之和为目标值的组合的方法。这些解决方案利用了哈希映射和双指针技术进行优化,部分情况使用了剪枝策略避免无效计算。
摘要由CSDN通过智能技术生成

 1.

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer,Integer> map = new HashMap<>();
        int count = 0;
        for (int i : nums1) {
            for (int j : nums2) {
                int temp = i + j;
                if (map.containsKey(temp)) {
                    map.put(temp, map.get(temp) + 1);
                } else {
                    map.put(temp, 1);
                }
            }
        }

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

2.

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] map = new int[26];
        for(char c : magazine.toCharArray()){
            map[c-'a']++;
        }
        for(char c : ransomNote.toCharArray()){
            map[c-'a']--;
        }
        for(int i : map){
            if(i<0){
                return false;
            }
        }
        return true;
    }
}

 3

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList();
        Arrays.sort(nums);
        int i,j;
        
        if(nums[0]>0 || nums.length<3){
            return ans;  //最小的数>0或者数字小于三个,直接返回空的ArrayList
        }
        for(int k=0;k<nums.length;k++){
            if(k > 0 && nums[k] == nums[k-1]) continue; // 重复解
            i = k+1;
            j = nums.length - 1;
            while(i<j){
                int sum = nums[k]+nums[i]+nums[j];
                if(sum==0){
                    ans.add(Arrays.asList(nums[k],nums[i],nums[j]));
                    while(i<j && nums[i] == nums[i+1]) i++;
                    while(i<j && nums[j] == nums[j-1]) j--;
                    i++;
                    j--;
                }else if(sum>0){
                    j--;
                }else if(sum<0){
                    i++;
                }

            }
        }
        return ans;
    }
}

4.

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++) {
		
            // 剪枝
            if (nums[i] > 0 && nums[i] > target) {
                return result;
            }
		
            if (i > 0 && nums[i - 1] == nums[i]) {    //nums[i]去重
                continue;
            }
            
            for (int j = i + 1; j < nums.length; j++) {

                if (j > i + 1 && nums[j - 1] == nums[j]) {  //nums[j]去重
                    continue;
                }

                int left = j + 1;
                int right = nums.length - 1;
                while (right > left) {
		           //需要转化为long类型 否则会溢出
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {   //等于target
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        //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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值