代码随想录二刷day07

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

文章目录

  • 前言
  • [一、力扣454. 四数相加 II](#一、力扣454. 四数相加 II)
  • [二、力扣383. 赎金信](#二、力扣383. 赎金信)
  • [三、力扣15. 三数之和](#三、力扣15. 三数之和)
  • [四、力扣18. 四数之和](#四、力扣18. 四数之和)

前言

提示:这里可以添加本文要记录的大概内容:


提示:以下是本篇文章正文内容,下面案例可供参考

一、力扣454. 四数相加 II

java 复制代码
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: nums1){
            for(int j : nums2){
                int sum = i + j;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }
        for(int i : nums3){
            for(int j : nums4){
                res += map.getOrDefault(0-i - j, 0);
            }
        }
        return res;
    }
}

二、力扣383. 赎金信

java 复制代码
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] arr = new int[27];
        for(int i = 0; i < ransomNote.length(); i ++){
            arr[ransomNote.charAt(i) - 'a'] ++;
        }
        for(int i = 0; i < magazine.length(); i ++){
            arr[magazine.charAt(i) - 'a'] --;
        }
        for(int i : arr){
            if(i > 0){
                return false;
            }
        }
        return true;
    }
}

三、力扣15. 三数之和

java 复制代码
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0; i < nums.length-2;){
            if(nums[i] > 0){
                return res;
            }
            int left = i+1, 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{
                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    while(left < right && nums[left] == nums[left + 1])left ++;
                    while(left < right && nums[right] == nums[right - 1]) right --;
                    left ++;
                    right --;
                }
            }
            while(nums[i] == nums[i+1] && i < nums.length-2)i ++;i ++;
        }
        return res;
    }
}

四、力扣18. 四数之和

java 复制代码
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums.length <= 3){
            return res;
        }
        Arrays.sort(nums);
        for(int i = 0; i < nums.length -3;){
            if(target < 0 && nums[i]>0){
                return res;
            }
            for(int j = i + 1; j < nums.length - 2; ){
                int left = j + 1, right = nums.length - 1;
                while(left < right){
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum > target){
                        right --;
                    }else if(sum < target){
                        left ++;
                    }else{
                        res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while(left < right && nums[left] == nums[left+1])left++;
                        while(left < right && nums[right] == nums[right-1])right--;
                        left ++;
                        right --;
                    }
                }
                while(j < nums.length-2 && nums[j] == nums[j + 1])j ++; j ++;
            }
            while(i < nums.length-3 && nums[i] == nums[i+1])i ++; i ++;
        }
        return res;
    }
}
相关推荐
月明长歌3 分钟前
怎么把 SQL 的增删改查写成“稳、准、可维护”的
java·数据库·sql
南汐以墨5 分钟前
UI自动化测试指南(二):常用方法
java·测试工具
UID96226 分钟前
[特殊字符] 无级变速传动(CVT)技术突破之道 | 易经×数学×工程的跨维度破解方案
算法·数学建模·开源
生信碱移12 分钟前
神经网络单细胞预后分析:这个方法直接把 TCGA 预后模型那一套迁移到单细胞与空转数据上了!竟然还能做模拟敲除与预后靶点筛选?!
人工智能·深度学习·神经网络·算法·机器学习·数据挖掘·数据分析
萧曵 丶13 分钟前
Java 泛型详解
java·开发语言·泛型
在风中的意志18 分钟前
[数据库SQL] [leetcode-175] 175. 组合两个表
数据库·sql·leetcode
圣保罗的大教堂18 分钟前
leetcode 1970. 你能穿过矩阵的最后一天 困难
leetcode
yugi98783823 分钟前
MFCC特征提取与SVM训练语音识别
算法·支持向量机·语音识别
独断万古他化25 分钟前
【Spring Web MVC 入门实战】实战三部曲由易到难:加法计算器 + 用户登录 + 留言板全流程实现
java·后端·spring·mvc
yuanmenghao27 分钟前
MSAC 算法详解以及与 RANSAC 对比示例
算法·自动驾驶·聚类·ransac·msac·系统辨识‘