Day8力扣打卡

打卡记录

查找和替换模式(哈希表 / find函数查询重复程度)

链接

1.hash表双映射检测是否存在相同映射。

2.利用string的find函数返回下标来检测对应字符串的重复程度(妙)。

cpp 复制代码
class Solution {
public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        auto match = [&](string& s, string& p) -> bool
        {
            unordered_map<char, char> hash;
            for (int i = 0; i < s.size(); ++i)
            {
                char x = s[i], y = p[i];
                if (!hash.count(x)) hash[x] = y;
                else if (hash[x] != y) return false;
            }
            return  true;
        };
        vector<string> ans;
        for (auto& word : words)
            if (match(word, pattern) && match(pattern, word)) ans.push_back(word);
        return ans;
    }
};
cpp 复制代码
class Solution {
public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
        vector<string> ans;
        auto match = [&](string& s, string& p) -> bool
        {
            for (int i = 0; i < s.size(); ++i)
                if (s.find(s[i]) != p.find(p[i])) return false;
            return true;
        };
        for (auto& word : words)
            if (match(word, pattern)) ans.push_back(word);
        return ans;
    }
};

划分数组使最大差为 K(排序 + 贪心 + 移动窗口)

链接

由于求子序列的最大值与最小值,因此其顺序可以打乱,可以直接使用sort快排,然后贪心采用移动窗口来求最小分组数。

cpp 复制代码
class Solution 
{
public:
    int partitionArray(vector<int>& nums, int k) 
    {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        int res = 0, l = 0, r = 0;
        while (r < n)
        {
            if (nums[l] + k >= nums[r]) r++;
            else 
            {
                res++;
                l = r++;
            }
        }
        if (l < r) res ++;
        return res;
    }
};
相关推荐
景彡先生2 小时前
C++并行计算:OpenMP与MPI全解析
开发语言·c++
归去_来兮3 小时前
深度学习模型在C++平台的部署
c++·深度学习·模型部署
黑听人4 小时前
【力扣 困难 C】329. 矩阵中的最长递增路径
c语言·leetcode
pay4fun4 小时前
2048-控制台版本
c++·学习
YuTaoShao5 小时前
【LeetCode 热题 100】141. 环形链表——快慢指针
java·算法·leetcode·链表
hjjdebug6 小时前
ffplay6 播放器关键技术点分析 1/2
c++·ffmpeg·音视频
小小小新人121236 小时前
C语言 ATM (4)
c语言·开发语言·算法
Azxcc07 小时前
C++异步编程入门
开发语言·c++
吐泡泡_7 小时前
C++(STL源码刨析/vector)
c++
你的冰西瓜7 小时前
C++排序算法全解析(加强版)
c++·算法·排序算法