day6 | 454.四数相加II 383. 赎金信 15. 三数之和 18. 四数之和

454.四数相加II

1、代码(BC)

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

2、代码(AC)

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

383. 赎金信

1、代码(AC)

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

2、代码2(AC)

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

2、分析

(1)类似于有效异位词,通过哈希的思想将下标与字母相联系,将值与次数相联系。

15. 三数之和

1、代码(BC)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<Integer> record = new List<>();
        int fast = 2;
        int mid = 1;
        int slow = 0;
        while(slow < nums.length - 2){
            while(mid < nums.length - 1){
                while(fast < nums.length ){
                    if(nums[slow] + nums[mid] + nums[fast] == 1){
                            record[0][0] = nums[slow];
                            record[0][1] = nums[mid];
                            record[0][2] = nums[fast];
                            //如何赋值?如何使用指针?
                    }else{
                        fast++;
                    }
                }
                mid++;
                fast = mid + 1;
            }
            slow++;
            mid = slow + 1;
            fast = mid + 1;
        }
        return record;
    }
}

2、代码(AC)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0; i<nums.length; i++){
            //根据题意剪枝操作
            //因为这是升序数组
            if(nums[i] > 0){
                return result;
            }
            //去重a -> 答案中不可以包含"数值"重复的三元组
            if(i>0 && nums[i] == nums[i-1]){
                continue;
            }
            int left = i +1;
            int right = nums.length - 1;
            while(left < right){
                int sum = nums[i] + nums[left] + nums[right];
                if(sum > 0){
                    right--;
                }else if(sum < 0){
                    left++;
                }else{
                    result.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    left++;
                    right--;
                    while(left<right && nums[left] == nums[left - 1]){
                        left++;
                    }
                    while(left<right && nums[right] == nums[right + 1]){
                        right--;
                    }
                }
            }
        }
        return result;
    }
}

2、分析

(1)题目使用相向双指针进行遍历,由于题干说答案中不可包含重复的三元组,所以在遍历时也需要进行去重操作。
(2)双指针遍历条件

while(left < right)

18. 四数之和

1、代码

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(left < right){
                    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]));
                        left++;
                        right--;
                        while(left < right && nums[left] == nums[left - 1]){
                            left++;
                        }
                        while(left < right && nums[right] == nums[right + 1]){
                            right--;
                        }
                    }
                }
            }
        }
        return result;
    }
}

2、分析

(1)题目注意点说到:若两个四元组元素一一对应,则认为两个四元组重复。它指的是元素而非下标(例如[2,2,2,2,2]的情况),并不用对齐下标,所以需要对每个指针进行去重。
(2)剪枝操作从目前情况来看,想得到就写。
(3)ArrayList。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值