力扣第 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. 增加操作后最大按位与的结果

相关推荐
在所不辞兄4 分钟前
为什么数字孪生驱动的产线标定需要在线更新机制,而不能一次标定后长期沿用?
人工智能·神经网络·其他·算法·机器学习
文丑颜不良啊4 小时前
LeetCode3401~3420题目解析
java·leetcode
AvocadoZeroOneOI5 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
文丑颜不良啊5 小时前
LeetCode3421~3440题目解析
java·leetcode
cjz38997 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
橘子1310 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘21 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴1 天前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David1 天前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
LK_071 天前
【力扣】142. 环形链表 II |Python
python·leetcode·链表