【算法刷题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;
    }
}

总结

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

相关推荐
_OP_CHEN3 分钟前
【算法基础篇】(五十八)线性代数之高斯消元法从原理到实战:手撕模板 + 洛谷真题全解
线性代数·算法·蓝桥杯·c/c++·线性方程组·acm/icpc·高斯消元法
YGGP9 分钟前
【Golang】LeetCode 1. 两数之和
leetcode
唐梓航-求职中10 分钟前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试
少许极端17 分钟前
算法奇妙屋(二十八)-递归、回溯与剪枝的综合问题 1
java·算法·深度优先·剪枝·回溯·递归
仰泳的熊猫19 分钟前
题目1453:蓝桥杯历届试题-翻硬币
数据结构·c++·算法·蓝桥杯
唐梓航-求职中19 分钟前
技术-算法-leetcode-1606. 找到处理最多请求的服务器(易懂版)
服务器·算法·leetcode
啊阿狸不会拉杆20 分钟前
《机器学习导论》第 10 章-线性判别式
人工智能·python·算法·机器学习·numpy·lda·线性判别式
会叫的恐龙21 分钟前
C++ 核心知识点汇总(第11日)(排序算法)
c++·算法·排序算法
twilight_46921 分钟前
机器学习与模式识别——线性回归算法
算法·机器学习·线性回归
玄同76528 分钟前
Python Random 模块深度解析:从基础 API 到 AI / 大模型工程化实践
人工智能·笔记·python·学习·算法·语言模型·llm