代码随想录四刷day6

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


两层for循环就可以确定 a 和b 的数值了,可以使用哈希法来确定 0-(a+b) 是否在 数组里出现过,其实这个思路是正确的,但是我们有一个非常棘手的问题,就是题目中说的不可以包含重复的三元组。

一、力扣1. 两数之和

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        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 ++){
            int temp = target - nums[i];
            if(map.containsKey(temp) && map.get(temp) != i){
                res[0] = map.get(temp);
                res[1] = i;
                return res;
            }
        }
        return res;
    }
}

二、力扣454. 四数相加 II

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

三、力扣383. 赎金信

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

四、力扣15. 三数之和

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值