Day07:454&383

454. 四数相加 II - 力扣(LeetCode)

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


    }
}

383. 赎金信 - 力扣(LeetCode)

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

    }
}

15. 三数之和 - 力扣(LeetCode) 

 

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res=new ArrayList<>();
        for(int k = 0; k < nums.length - 2; k++){
            if(nums[k] > 0) break;
            if(k > 0 && nums[k] == nums[k - 1]) continue;
            int i = k + 1, j = nums.length - 1;
            while(i < j){
                int sum = nums[k] + nums[i] + nums[j];
                if(sum < 0){
                    while(i < j && nums[i] == nums[++i]);
                } else if (sum > 0) {
                    while(i < j && nums[j] == nums[--j]);
                } else {
                    res.add(new ArrayList<Integer>(Arrays.asList(nums[k], nums[i],nums[j])));
                    while(i < j && nums[i] == nums[++i]);
                    while(i < j && nums[j] == nums[--j]);
                }
            }
        }
        return res;
    }
}

 18. 四数之和 - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
     List<List<Integer>> res  =  new ArrayList<>();
     Arrays.sort(nums);
     int len = nums.length;
     for(int i = 0; i < len - 3; i++) {   
         if (i > 0 && nums[i] == nums[i-1]) {
             continue; 
         }
         for (int j = i + 1; j < len - 2; j++) { 
             if (j > i + 1 && nums[j] == nums[j-1]) {
                 continue; 
             }
             int left = j + 1;
             int right = len - 1;
             while (left < right) {
                 long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                 if (sum == target) {
                     res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));       
                     while(left < right && nums[left+1] == nums[left]) left++;
                     while (left < right && nums[right-1] == nums[right]) right--;    
                     left++;
                     right--;
                 } else if (sum > target) {
                     right--;
                 } else { 
                     left++;
                 }
             }
         }
     }

     return res;

    }
}

 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值