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

相关推荐
踩坑记录18 小时前
leetcode hot100 easy 101. 对称二叉树 递归 层序遍历 bfs
算法·leetcode·宽度优先
2501_9403152619 小时前
leetcode182动态口令(将字符的前几个元素放在字符串后面)
算法
老鼠只爱大米19 小时前
LeetCode经典算法面试题 #98:验证二叉搜索树(递归法、迭代法等五种实现方案详解)
算法·leetcode·二叉树·递归·二叉搜索树·迭代
疯狂的喵1 天前
C++编译期多态实现
开发语言·c++·算法
scx201310041 天前
20260129LCA总结
算法·深度优先·图论
2301_765703141 天前
C++中的协程编程
开发语言·c++·算法
m0_748708051 天前
实时数据压缩库
开发语言·c++·算法
小魏每天都学习1 天前
【算法——c/c++]
c语言·c++·算法
智码未来学堂1 天前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
Halo_tjn1 天前
基于封装的专项 知识点
java·前端·python·算法