第 383 场 LeetCode 周赛题解

A 边界上的蚂蚁

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

模拟

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 将单词恢复初始状态所需的最短时间 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 找出网格的区域平均强度

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

模拟:枚举网格中的 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 将单词恢复初始状态所需的最短时间 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;
        }
    };
};

相关推荐

  1. LeetCode 384个人题解

    2024-02-07 22:30:02       40 阅读
  2. LeetCode 385个人题解

    2024-02-07 22:30:02       36 阅读
  3. LeetCode 388 个人题解

    2024-02-07 22:30:02       21 阅读
  4. LeetCode389个人题解

    2024-02-07 22:30:02       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-07 22:30:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-07 22:30:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-07 22:30:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-07 22:30:02       20 阅读

热门阅读

  1. CGAL::2D Arrangements-5

    2024-02-07 22:30:02       29 阅读
  2. c++学习:基本变量类型+宽字符用法

    2024-02-07 22:30:02       28 阅读
  3. MySQL中常用的数据类型

    2024-02-07 22:30:02       27 阅读
  4. 力扣_字符串4—编辑距离

    2024-02-07 22:30:02       30 阅读
  5. 手写AJAX

    2024-02-07 22:30:02       32 阅读
  6. 2024/2/6

    2024-02-07 22:30:02       35 阅读
  7. MySQL进阶查询篇(1)-索引的类型与创建

    2024-02-07 22:30:02       31 阅读
  8. 9种chrome有趣的小众插件

    2024-02-07 22:30:02       39 阅读
  9. CGAL::2D Arrangements-8

    2024-02-07 22:30:02       34 阅读
  10. 文件基础 (进程的基石)

    2024-02-07 22:30:02       39 阅读
  11. 校园自助洗浴设施运维服务认证的介绍

    2024-02-07 22:30:02       37 阅读
  12. 三、05 ansible基础命令ansible 常用命令

    2024-02-07 22:30:02       29 阅读
  13. rac oracle安装流程

    2024-02-07 22:30:02       36 阅读