代码随想录day7|454. 四数相加II,383. 赎金信,15. 三数之和18. 四数之和

代码随想录day7|454. 四数相加II,383. 赎金信,15. 三数之和18. 四数之和

第一思路:两个hashmap分别算出nums1,nums2和nums3,nums4的和,再遍历查找

454. 四数相加 II

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

刚开始又看错题了,以为和为指定数,结果和为0就行

思路:用map记录下nums1和nums2的以及该出现的次数,再遍历nums3,nums4累加上符合条件的数就行

383. 赎金信

出现字母就既可以使用哈希又可以用数组

//数组法
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] mag = new int[26];
        for(int i = 0 ; i < magazine.length() ; i++){
            mag[magazine.charAt(i) - 'a']++;
        }
        for(int i = 0 ;i < ransomNote.length() ; i++){
            if(mag[ransomNote.charAt(i)-'a'] < 1)return false;
            mag[ransomNote.charAt(i)-'a']--;
        }
        return true;
    }
}
//哈希法
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        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++){
            if(map.get(ransomNote.charAt(i))==null||map.get(ransomNote.charAt(i))<1)return false;
            map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);
        }
        return true;
    }
}
15. 三数之和
//错误思路
//双循环+map
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        Map<Integer , Integer> map = new HashMap<>();
        for(int i = 0 ; i < nums.length;i++){
            map.put(-nums[i],i);
        }
        for(int i = 0 ; i < nums.length ; i++){
            for(int j = i+1 ; j < nums.length ; j++){
                if(map.containsKey(nums[i]+nums[j]) 
                && map.get(nums[i]+nums[j]) != i 
                && map.get(nums[i]+nums[j]) != j){
                List<Integer> temp =new ArrayList<>();
                temp.add(nums[i]);
                temp.add(nums[j]);
                temp.add(nums[map.get(nums[i]+nums[j])]);
                ans.add(temp);
                }
                
            }
        }
        return ans;
    }
}
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        for(int i = 0 ; i < nums.length && nums[i] <= 0 ; i++){
            int left = i + 1;
            int right = nums.length-1;
            //i去重,nums[i]==nums[i-1]是判断组外是否重复,nums[i]==nums[i+1]是判断组内是否重复
            if(i>0 &&nums[i]==nums[i-1])continue;
            //不能为(left < right)因为当right==left是不能加入循环
            while(left < right){
                if(nums[right]+nums[left] == -nums[i]){
                    
                    ans.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++;
                }else if(nums[right]+nums[left] > -nums[i]){
                    right --;
                }else{
                    left++;
                }
            }
        }
        return ans;
    }
}

18. 四数之和
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);

        for(int i = 0 ; i < nums.length ; i++){

           //去重
            if(nums[i] > 0 && nums[i] > target)return ans;
            if(i > 0&&nums[i] == nums[i - 1])continue;
            
            for(int j = i+1 ; j < nums.length; j++){

                if(j > i + 1 && nums[j] == nums[j - 1])continue;

                int left = j + 1;
                int right = nums.length - 1;
                while(right > left){
                    long sum = (long)(nums[i] + nums[j] + nums[left] + nums[right]);
                     if (sum > target) {
                        right--;
                    } else if (sum < target) {
                        left++;
                    } else {
                        ans.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 ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值