第 383 场 LeetCode 周赛题解

news/2024/5/18 23:03:31 标签: leetcode, 算法, 模拟, 枚举, 字符串哈希

A leetcode.cn/problems/ant-on-the-boundary/description/" rel="nofollow">边界上的蚂蚁

在这里插入图片描述
在这里插入图片描述

模拟

class Solution {
public:
    int returnToBoundaryCount(vector<int> &nums) {
        int s = 0;
        int res = 0;
        for (auto x: nums) {
            s += x;
            if (s == 0)
                res++;
        }
        return res;
    }
};

B leetcode.cn/problems/minimum-time-to-revert-word-to-initial-state-i/description/" rel="nofollow">将单词恢复初始状态所需的最短时间 I

在这里插入图片描述
在这里插入图片描述

枚举:若经过 i i i 秒后 w o r d word word 可以恢复到其初始状态,则说明 w o r d word word 长为 n − i × k n-i\times k ni×k的后缀与长为 n − i × k n-i\times k ni×k的前缀相等, n n n w o r d word word 的长度,升序枚举 i i i 直到满足条件

class Solution {
public:
    int minimumTimeToInitialState(string word, int k) {
        int n = word.size();
        int i = 1;
        for (; i * k < n; i++) {
            int tag = 1;
            for (int j = i * k; j < n; j++)
                if (word[j] != word[j - i * k])
                    tag = 0;
            if (tag)
                return i;
        }
        return i;
    }
};

C leetcode.cn/problems/find-the-grid-of-region-average/description/" rel="nofollow">找出网格的区域平均强度

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

模拟枚举网格中的 3 × 3 3\times3 3×3 的子网格,判断是否是区域,同时记录各位置所属于的区域数和所属于的区域的平均强度之和,最后求网格的区域平均强度

class Solution {
public:
    vector<vector<int>> resultGrid(vector<vector<int>> &image, int threshold) {
        int m = image.size(), n = image[0].size();
        vector<vector<int>> tot(m, vector<int>(n));
        vector<vector<int>> cnt(m, vector<int>(n));
        for (int i = 0; i + 2 < m; i++)
            for (int j = 0; j + 2 < n; j++) {
                int tag = 1;//判断是否是区域
                for (int x = i; x < i + 3; x++)
                    for (int y = j + 1; y < j + 3; y++)
                        if (abs(image[x][y] - image[x][y - 1]) > threshold)
                            tag = 0;
                for (int y = j; y < j + 3; y++)
                    for (int x = i + 1; x < i + 3; x++)
                        if (abs(image[x][y] - image[x - 1][y]) > threshold)
                            tag = 0;
                if (tag) {
                    int s = 0;
                    for (int x = i; x < i + 3; x++)
                        for (int y = j; y < j + 3; y++)
                            s += image[x][y];
                    s /= 9;//当前区域的平均强度
                    for (int x = i; x < i + 3; x++)
                        for (int y = j; y < j + 3; y++) {
                            cnt[x][y]++;//所属区域数目+1
                            tot[x][y] += s;//所属区域的平均强度之和+s
                        }
                }
            }
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (!cnt[i][j])
                    tot[i][j] = image[i][j];
                else
                    tot[i][j] /= cnt[i][j];
        return tot;
    }
};

D leetcode.cn/problems/minimum-time-to-revert-word-to-initial-state-ii/description/" rel="nofollow">将单词恢复初始状态所需的最短时间 II

在这里插入图片描述
在这里插入图片描述

字符串哈希 + 枚举:若经过 i i i 秒后 w o r d word word 可以恢复到其初始状态,则说明 w o r d word word 长为 n − i × k n-i\times k ni×k的后缀与长为 n − i × k n-i\times k ni×k的前缀相等, n n n w o r d word word 的长度,升序枚举 i i i 直到满足条件

class Solution {
public:
    int minimumTimeToInitialState(string word, int k) {
        int n = word.size();
        int i = 1;
        srand(time(0));
        shash h(word, 2333 + rand() % 100, 1e9 + rand() % 100);
        for (; i * k < n; i++) {     
            if (h(i * k, n - 1) == h(0, n - 1 - i * k))//s[i*k,n-1]和s[0,n-1-i*k]是否相等
                return i;
        }
        return i;
    }

    class shash {//字符串哈希模板
    public:
        using ll = long long;
        vector<ll> pres;
        vector<ll> epow;
        ll e, p;

        shash(string &s, ll e, ll p) {
            int n = s.size();
            this->e = e;
            this->p = p;
            pres = vector<ll>(n + 1);
            epow = vector<ll>(n + 1);
            epow[0] = 1;
            for (int i = 0; i < n; i++) {
                pres[i + 1] = (pres[i] * e + s[i]) % p;
                epow[i + 1] = (epow[i] * e) % p;
            }
        }

        ll operator()(int l, int r) {
            ll res = (pres[r + 1] - pres[l] * epow[r - l + 1] % p) % p;
            return (res + p) % p;
        }
    };
};

http://www.niftyadmin.cn/n/5374533.html

相关文章

rtt设备io框架面向对象学习-看门狗设备

1.看门狗设备基类 / components / drivers / include / drivers /下的watchdog.h 定义了如下看门狗设备基类 struct rt_watchdog_device { struct rt_device parent; const struct rt_watchdog_ops *ops; }; 看门狗设备基类的方法定义如下 struct rt_watchdog_ops { rt_err_…

【lesson51】信号之信号处理

文章目录 信号处理可重入函数volatileSIGCHLD信号 信号处理 信号产生之后&#xff0c;信号可能无法被立即处理&#xff0c;一般在合适的时候处理。 1.在合适的时候处理&#xff08;是什么时候&#xff1f;&#xff09; 信号相关的数据字段都是在进程PCB内部。 而进程工作的状态…

Mybatis开发辅助神器p6spy

Mybatis什么都好&#xff0c;就是不能打印完整的SQL语句&#xff0c;虽然可以根据数据来判断一二&#xff0c;但始终不能直观的看到实际语句。这对我们想用完整语句去数据库里执行&#xff0c;带来了不便。 怎么说呢不管用其他什么方式来实现完整语句&#xff0c;都始终不是Myb…

PMP-情景模拟学习法-识别时间点

概述 情景模拟的总体思路为“三个识别”&#xff1b; 识别提问的点&#xff0c;也就是题目立足于哪个时间点要求你解决的问题&#xff0c;是面向现在、面向过去&#xff0c;还是面向未来&#xff1f;这是理解提问逻辑的前提&#xff1b;识别项目所在的阶段&#xff0c;不同阶段…

给定具体日期 返回给定日期是星期几 calendar.weekday(year,month,day)

【小白从小学Python、C、Java】 【计算机等考500强证书考研】 【Python-数据分析】 给定具体日期 返回给定日期是星期几 calendar.weekday(year,month,day) [太阳]选择题 如果2024年2月12日是星期一&#xff0c;请问最后一个print语句的运行结果是&#xff1f; import calenda…

《Docker极简教程》--Docker基础--基础知识(三)

一、Namespace和Cgroups 1.1 Namespace的作用和类型 Docker 的 Namespace 是 Linux 内核提供的一种机制&#xff0c;用于隔离系统资源&#xff0c;使得容器能够拥有自己独立的视图&#xff0c;从而实现更高程度的隔离和安全性。Namespace 在 Docker 中扮演着至关重要的角色&a…

安装flash-attention失败的终极解决方案

安装大语言模型的时候&#xff0c;有时候需要安装flash-attention来加速。比如说通义千问里的安装方法&#xff1a; git clone https://github.com/Dao-AILab/flash-attention cd flash-attention && pip install . 我们经常安着安着就卡住了&#xff0c;比如说下面的…

Debezium发布历史123

原文地址&#xff1a; https://debezium.io/blog/2022/06/02/debezium-1-9-3-final-released/ 欢迎关注留言&#xff0c;我是收集整理小能手&#xff0c;工具翻译&#xff0c;仅供参考&#xff0c;笔芯笔芯. Debezium 1.9.3.Final Released June 2, 2022 by Chris Cranford r…