【算法刷题day7】Leetcode:454. 四数相加 II、383. 赎金信、15. 三数之和、18. 四数之和

文章目录

草稿图网站
java的Deque

Leetcode 454. 四数相加 II

题目: 454. 四数相加 II
解析: 代码随想录解析

解题思路

使用HashMap来记录nums1+nums2的所有情况,寻找-(nums3+nums4)的次数。

代码

java 复制代码
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int numI : nums1)
            for(int numJ : nums2){
                int sum = numI + numJ;
                map.put(sum, map.getOrDefault(sum, 0) + 1);//getOrDefault如果找到返回sum,否则返回0
            }
        int res = 0;
        for (int numI : nums3)
            for (int numJ : nums4){
                int sum = numI + numJ;
                res += map.getOrDefault(-sum, 0);
            }
        return res;
    }
}

总结

暂无

Leetcode 383. 赎金信

题目: 383. 赎金信
解析: 代码随想录解析

解题思路

HashMap来记录和删除。

代码

java 复制代码
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++){
            int c = magazine.charAt(i);
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++){
            int c = ransomNote.charAt(i);
            int count = map.getOrDefault(c, 0);
            if (count == 0)
                return false;
            map.put(c, count - 1);
        }
        return true;
    }
}

总结

这题可以用Hash数组来解决

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

Leetcode 15. 三数之和

题目: 15. 三数之和
解析: 代码随想录解析

解题思路

自己没想出来,参考了Carl的代码。

遍历数组,设定最小的i,然后使用双指针寻找到所有可能的结果。

代码

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; i++){
            if (nums[i] > 0)
                break;
            // 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){
                    left++;
                }else if (sum > 0){
                    right--;
                }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 (i < nums.length - 1 && nums[i] == nums[i+1])
               i++;
        }
        return res;
    }
}

总结

暂无

Leetcode 18. 四数之和

题目: 18. 四数之和
解析: 代码随想录解析

解题思路

两层循环后,双指针遍历。

continue条件要想到元素大于target的同时,要大于等于0才能break

代码

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

总结

要想到和三数之和是一个道理

相关推荐
yzx99101311 分钟前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题14 分钟前
算法练习-回溯
算法
眼镜哥(with glasses)1 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
int型码农6 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT6 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面6 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked936 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,6 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵6 小时前
有效的括号题解
数据结构·算法·
GIS小天7 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票