• Day8力扣打卡


    打卡记录

    在这里插入图片描述


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

    链接

    1.hash表双映射检测是否存在相同映射。
    2.利用string的find函数返回下标来检测对应字符串的重复程度(妙)。

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

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

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    box-sizing: border-box;box-sizing:content-box;讲解
    LC118原厂直流驱动芯片 SOP-8
    DQN论文阅读
    Docker 入门流程
    【Matlab】数据统计分析
    大公司为什么禁止SpringBoot项目使用Tomcat?
    nanomsg下载、安装、测试(一)
    python-(6-2)爬虫---小试牛刀,获得网页页面内容
    第十二章:泛型(Generic)
    需求评审失败,常见的5大缺陷。
  • 原文地址:https://blog.csdn.net/qq947467490/article/details/134001604