代码随想录二刷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;
    }
}
相关推荐
yuuki2332333 分钟前
【数据结构】用顺序表实现通讯录
c语言·数据结构·后端
DashVector26 分钟前
向量检索服务 DashVector产品计费
数据库·数据仓库·人工智能·算法·向量检索
AI纪元故事会27 分钟前
【计算机视觉目标检测算法对比:R-CNN、YOLO与SSD全面解析】
人工智能·算法·目标检测·计算机视觉
夏鹏今天学习了吗32 分钟前
【LeetCode热题100(59/100)】分割回文串
算法·leetcode·深度优先
卡提西亚35 分钟前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实35 分钟前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
陈果然DeepVersion1 小时前
Java大厂面试真题:Spring Boot+Kafka+AI智能客服场景全流程解析(十)
java·spring boot·ai·kafka·面试题·向量数据库·rag
但要及时清醒2 小时前
ArrayList和LinkedList
java·开发语言
一叶飘零_sweeeet2 小时前
从测试小白到高手:JUnit 5 核心注解 @BeforeEach 与 @AfterEach 的实战指南
java·junit