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;
    }
};
相关推荐
Yue丶越3 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
小白程序员成长日记3 小时前
2025.11.24 力扣每日一题
算法·leetcode·职场和发展
有一个好名字3 小时前
LeetCode跳跃游戏:思路与题解全解析
算法·leetcode·游戏
爱学习的小邓同学4 小时前
C++ --- 多态
开发语言·c++
AndrewHZ4 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
蓝牙先生4 小时前
简易TCP C/S通信
c语言·tcp/ip·算法
2501_941870565 小时前
Python在高并发微服务数据同步与分布式事务处理中的实践与优化
leetcode
xiaoye-duck7 小时前
计数排序:高效非比较排序解析
数据结构
2501_941147717 小时前
高并发微服务架构Spring Cloud与Dubbo在互联网优化实践经验分享
leetcode
稚辉君.MCA_P8_Java8 小时前
Gemini永久会员 Java中的四边形不等式优化
java·后端·算法