代码随想录算法训练营第七天 | 454. 四数相加 II、383. 赎金信、15. 三数之和、18. 四数之和

454. 四数相加 II

题解及想法

通过两个for循环先遍历a和b,key放a和b两数之和,value 放a和b两数之和出现的次数,再通过两个for循环遍历c和d,如果0-(c+d) 在map中出现过的话,就用res把map中key对应的value也就是出现次数统计出来。

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

            for (int i = 0; i < nums1.length; i++){
                for (int j = 0; j < nums1.length; j++){
                    temp = nums3[i] + nums4[j];
                    if(map1.containsKey(0 - temp)) {
                        res += map1.get(0 - temp);
                    }
                }
            }
            return res;
        }
    }

383. 赎金信

题解及想法

将magazine字符串保存在数组中,再遍历ransomNote,将ransomNote中出现的字符在数组中减去,如果数组中出现负数,也就是ransomNote中出现的某一个字符多于在magazine中的字符,即ransomNote不能由magazine包含,反之即可。

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

15. 三数之和

题解及想法

本题我没有想出来,看了题解才恍然大悟,其中的去重是重点,还需要再细细品味。

 如图所示,先排序,设置指针i遍历数组,再设置left和right两个指针,一个指向i + 1,一个指向nums.length-1。如果nums[i] + nums[left] + nums[right] > 0 则三数之和大了,因为数组是排序后了,所以right下标就应该向左移动,这样才能让三数之和小一些。如果 nums[i] + nums[left] + nums[right] < 0 说明 此时三数之和小了,left 就向右移动,才能让三数之和大一些,直到left与right相遇为止。

详情请看代码随想录

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0; i < nums.length; i++){
            
            if( nums[i] > 0 ){
                return list;
            }
     //这个可以continue得原因是因为前面结果集已经有了对应元素,比如当前是-1,
     //已经把能凑到0的结果全搞出来了,当你下一个元素还是-1.结果集都是一样的就去重了
            if(i > 0 && nums[i] == nums[i - 1]){  
                continue;                        
            }
            
            int left = i + 1;
            int right = nums.length - 1;
            while(left < right){
                if(nums[i] + nums[left] + nums[right] > 0){
                    right--;
                }else if(nums[i] + nums[left] + nums[right] < 0){
                    left++;
                }else {
                    list.add(Arrays.asList(nums[i], nums[left], nums[right]));
                     // 去重逻辑应该放在找到一个三元组之后,对b 和 c去重
                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    
                    left++;
                    right--;
                }
            }
        }
        return list;
    }
}

18. 四数之和

题解及想法

与上一题类似,多加一层循环,特别要注意去重和剪枝的边界问题。

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] > target && nums[i] > 0){
                return result;
            }
            if(i > 0 && nums[i] == nums[i - 1]){   // 对nums[i]去重
                continue;
            }
            for(int j = i + 1; j < nums.length; j++){
                // int sum = nums[i] + nums[j];
                // if(sum > target && target > 0){
                //     return result;
                // }
                if(j > i + 1 && nums[j] == nums[j - 1]){ // 对nums[j]去重
                    continue;
                }
                int left = j + 1,right = nums.length - 1;
                while(right > left){
                    // nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum > target){
                        right--;
                    }else if(sum < target){
                        left++;
                    }else{
                        result.add(Arrays.asList(nums[i], nums[j], 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、付费专栏及课程。

余额充值