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

相关推荐
YuTaoShao2 小时前
【LeetCode 每日一题】865. 具有所有最深节点的最小子树——(解法一)自顶向下
算法·leetcode·职场和发展
2301_800895102 小时前
hh的蓝桥杯每日一题--拔河
职场和发展·蓝桥杯
CCPC不拿奖不改名2 小时前
计算机网络:电脑访问网站的完整流程详解+面试习题
开发语言·python·学习·计算机网络·面试·职场和发展
寻星探路2 小时前
【算法专题】哈希表:从“两数之和”到“最长连续序列”的深度解析
java·数据结构·人工智能·python·算法·ai·散列表
!停3 小时前
C语言单链表
c语言·数据结构·算法
独自破碎E3 小时前
【队列】求二叉树的层序遍历
leetcode
闻缺陷则喜何志丹3 小时前
【回文 字符串】3677 统计二进制回文数字的数目|2223
c++·算法·字符串·力扣·回文
Tisfy3 小时前
LeetCode 0085.最大矩形:单调栈
算法·leetcode·题解·单调栈
mit6.8243 小时前
出入度|bfs|状压dp
算法