【算法日记】力扣454、383

力扣454.四数相加 II

题目描述

给你四个整数数组 nums1nums2nums3nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

示例 1:

输入:nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
输出:2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0

示例 2:

输入:nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
输出:1

提示:

  • n == nums1.length
  • n == nums2.length
  • n == nums3.length
  • n == nums4.length
  • 1 <= n <= 200
  • -228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228

思路

用哈希表的思路,前两个数和后两个数一组。前面这组数加和添加到map中,至于为什么是unordered_map,因为unordered_map底层使用哈希表实现而且可以输入pair对。为什么是两个数一组?因为需要使用嵌套for循环向unordered_map输入和数,四个数分为两组时间复杂度可以控制在O(n^2)。第二个嵌套for循环负责寻找后两个数的和差的数,若有就把map中的值加到count结果里。

源码

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        unordered_map<int, int> umap;
        int count = 0;
        for (int n1 : nums1) {
            for (int n2: nums2) {
                umap[n1 + n2]++;
            }
        }
        for (int n3 : nums3) {
            for (int n4 : nums4) {
                if (umap.find(0 - (n3 + n4)) != umap.end()) {
                    count += umap[0 - (n3 + n4)];
                }
            }
        }
        return count;
    }
};

力扣383.赎金信

题目描述

给你两个字符串:ransomNotemagazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。

如果可以,返回 true ;否则返回 false

magazine 中的每个字符只能在 ransomNote 中使用一次。

示例 1:

输入:ransomNote = "a", magazine = "b"
输出:false

示例 2:

输入:ransomNote = "aa", magazine = "ab"
输出:false

示例 3:

输入:ransomNote = "aa", magazine = "aab"
输出:true

提示:

  • 1 <= ransomNote.length, magazine.length <= 105
  • ransomNotemagazine 由小写英文字母组成

思路

哈希表

源码

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<int, int> umap;
        for (char m : magazine) {
            umap[m-97]++;
        }
        for (char r : ransomNote) {
            auto iter = umap.find(r-97);
            if (iter != umap.end()) {
                int curr = --umap[r-97];
                if (curr < 0) return false;
            } else {
                return false;
            }
        }
        return true;
    }
};
### 前端算法LeetCode 练习资源 对于希望在 LeetCode 平台上寻找前端相关的算法题目练习资源的人而言,可以关注特定类型的题目来提升技能。LeetCode 提供了一个丰富的在线编程挑战库,其中涵盖了多种数据结构和算法问题。 #### 数据结构与算法基础 了解并掌握基本的数据结构如数组、链表、栈、队列以及哈希表是非常重要的。例如,在处理括号匹配的问题时,使用栈是一种常见而有效的解决方案[^3]: ```javascript function isValid(s) { const stack = []; const map = { '(': ')', '[': ']', '{': '}' }; for (const char of s) { if (char in map) { stack.push(char); } else if (!stack.length || map[stack.pop()] !== char) { return false; } } return !stack.length; } ``` #### 实际应用案例 针对更复杂的场景,比如实现自定义的哈希表,可以通过如下方式构建简单的键值存储系统[^4]: ```javascript class HashTable { constructor() { this.table = {}; } hashCode(key) { let hash = 0; for (let i = 0; i < key.length; i++) { hash += key.charCodeAt(i); } return hash % 100; // 使用模运算限制索引范围 } set(key, value) { const index = this.hashCode(key); this.table[index] = value; } get(key) { const index = this.hashCode(key); return this.table[index]; } } const ht = new HashTable(); ht.set('name', 'Alice'); console.log(ht.get('name')); // 输出 Alice ``` #### 题目推荐 为了更好地准备面试或者提高编码能力,可以从以下几个方面入手挑选适合自己的题目进行训练: - **字符串操作**:涉及字符转换、子串查找等问题。 - **动态规划**:适用于求解最优化路径类的任务。 - **树形结构遍历**:包括二叉树的各种遍历方法及其变种形式。 - **图论基础知识**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值