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

454

主要思路是在记录前两个数组中数字的和,放进hashmap,key和value一个记录和,另外一个记录次数。另外,再找出后面两个数组每个的和,然后用(0-sum2)去看能不能前面记录的凑成0;

Code:

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        HashMap<Integer, Integer> map1 = new HashMap<Integer, Integer>();
        int length = nums1.length;
        int result = 0;
        //first two group
        for(int i=0; i<length; i++){
            for(int j=0; j<length; j++){
                int sum1 = nums1[i] + nums2[j];
                map1.put(sum1, map1.getOrDefault(sum1, 0)+1);
            }
        }

        //last two group
        for(int i=0; i<length; i++){
            for(int j=0; j<length; j++){
                int sum2 = nums3[i] + nums4[j];
                int require = 0 -sum2;
                if(map1.containsKey(require)){
                    result = result + map1.get(require);
                }
            }
        }
        return result;
    }
}

383

运用hash数组的逻辑,第一步hash[]里面存ransomNote里面出现字母的次数,然后再loop一遍magazine的字母,然后在每个位置上-1。如果hash[]里面有位置的number是>0的,就证明ransomNote里面有字母是在magazine里面没有的,要return false。

Code:

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

15

掌握双指针思路,以及查重为什么要用nums[i] == nums[i-1]

Code:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        //sort first
        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){
                if(nums[i] + nums[left] + nums[right] > 0){
                    right--;
                }else if(nums[i] + nums[left] + nums[right] < 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

逻辑和上一题一样,只是多加了一个for loop。同样,一会碰到sum5, sum6,都是在里面多叠加一个for loop。只是要注意一些细节。

Code:

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]){
               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){
                    if(nums[i] + nums[j] + nums[left] + nums[right] > target){
                        right--;
                    }else if(nums[i] + nums[j] + nums[left] + nums[right] < 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++;
                        }
                        right--;
                        left++;
                    }
                }
           }
       }
       return result;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值