力扣第 484 场周赛

https://leetcode.cn/contest/weekly-contest-484/

目录

总结

[Q1. 统计残差前缀](#Q1. 统计残差前缀)

[Q2. 中心子数组的数量](#Q2. 中心子数组的数量)

[Q3. 统计凯撒加密对数目](#Q3. 统计凯撒加密对数目)

[Q4. 增加操作后最大按位与的结果](#Q4. 增加操作后最大按位与的结果)


总结

理解能力有待提高

哈希表应用刷题

map和unordered_map区别

Q1. 统计残差前缀

不同字符数量==前缀长度%3

cpp 复制代码
class Solution {
public:
    int residuePrefixes(string s) {
        unordered_map<char,int> map;
        int ans=0;
        for(int i=0;i<s.size();i++){
            map[s[i]]++;
            if(map.size()==(i+1)%3)ans++;
        }
        return ans;
    }
};

Q2. 中心子数组的数量

cpp 复制代码
class Solution {
public:
    int centeredSubarrays(vector<int>& nums) {
        int ans=0;
        for(int left=0;left<nums.size();left++){
            unordered_map<int,int> map;
            int sum=0;
            for(int right=left;right<nums.size();right++){
                map[nums[right]]++;
                sum+=nums[right];
                if(map[sum])ans++;
            }
        }
        return ans;
    }
};

Q3. 统计凯撒加密对数目

思路正确但超时O(n²)

cpp 复制代码
class Solution {
public:
    long long countPairs(vector<string>& words) {
        vector<vector<int>> s(words.size());
        for(int i=0;i<words.size();i++){
            int len=words[i].size();
            s[i].resize(len);
            s[i][0]=0;//第0位统一为0
            for(int j=1;j<len;j++){
                s[i][j]=(words[i][j]-words[i][j-1]+26)%26;
            }
        }
        long long ans=0;
        for(int i=0;i<s.size();i++){
            for(int j=i+1;j<s.size();j++){
                if(s[i]==s[j])ans++;
            }
        }
        return ans;
    }
};

优化:改变存储方式

map优化

cpp 复制代码
class Solution {
public:
    long long countPairs(vector<string>& words) {
        map<vector<int>, long long> cnt;
        for (string& s: words) {
            int len=s.size();
            vector<int> key(len);
            key[0]=0;
            for(int i=1;i<len;i++){
                key[i]=(s[i]-s[i-1]+26)%26;
            }
            cnt[key]++;
        }
        long long ans=0;
        for(auto &[key,value]:cnt){
            ans+=value*(value-1)/2;
        }
        return ans;
    }
};

string优化

cpp 复制代码
class Solution {
public:
    long long countPairs(vector<string>& words) {
        unordered_map<string,long long>cnt;
        for (string& s: words) {
            int len=s.size();
            string key="";
            key+='0';
            for(int i=1;i<len;i++){
                int t=(s[i]-s[i-1]+26)%26;
                key+='#';
                key+=to_string(t);
            }
            cnt[key]++;
        }
        long long ans=0;
        for(auto &[key,value]:cnt){
            ans+=value*(value-1)/2;
        }
        return ans;
    }
};

Q4. 增加操作后最大按位与的结果

相关推荐
Wect6 小时前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
NAGNIP18 小时前
一文搞懂深度学习中的通用逼近定理!
人工智能·算法·面试
颜酱1 天前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub1 天前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub1 天前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP2 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP2 天前
一文搞懂激活函数!
算法·面试