leetcode第119场双周赛 - 2023 - 12 - 9

比赛地址 :

https://leetcode.cn/contest/biweekly-contest-119/

t1 :

直接哈希表 加 暴力 统计就行了

复制代码
class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int> mp1,mp2;
        int n = nums1.size() , m = nums2.size();
        for(int& x : nums1) mp1[x]++;
        for(int& x : nums2) mp2[x]++;
        int a = 0 ,b = 0 ;
        for(int i=0;i<n;i++){
            if(mp2.find(nums1[i])!=mp2.end()){
                a++;
            }
        }
        for(int j = 0;j<m;j++){
            if(mp1.find(nums2[j])!=mp1.end()){
                b++;
            }
        }
        vector<int> ans;
        ans.push_back(a);
        ans.push_back(b);
        return ans;
    }
};

t2

直接模拟即可

复制代码
class Solution {
public:
    bool pd(char a, char b){
        if(a==b) return true;
        else if(a==b-1 || a==b+1) return true;
        else return false;
    }
    int removeAlmostEqualCharacters(string w) {
        // 直接模拟即可
        int n = w.size();
        int ans = 0;
        for(int i=0;i<n;i++){
            int j = i+1;
            while(j<n && pd(w[j-1],w[j])) j++;
            int len = j - i ;
            ans += len / 2;
            i = j - 1 ;
        }
        return ans;
    }
};

t3

直接滑动窗口来记录每个数的频次,维护一个滑动窗口满足题目条件;

复制代码
class Solution {
public:
    int maxSubarrayLength(vector<int>& nums, int k) {
        int n = nums.size();
        int l = 0 , r = 0 ;
        int ans = 0 ;
        unordered_map<int,int> mp;
        while(r < n){
            mp[nums[r]]++;
            while(mp[nums[r]]>k){
                mp[nums[l++]]--;
            }
            ans = max(ans,r-l+1);
            r ++;
        }
        return ans;
    }
};

t4

相关推荐
studyForMokey5 小时前
【Android面试】View绘制流程专题
android·面试·职场和发展
发疯幼稚鬼5 小时前
大整数乘法运算
c语言·算法
宵时待雨6 小时前
C++笔记归纳17:哈希
数据结构·c++·笔记·算法·哈希算法
酉鬼女又兒6 小时前
零基础快速入门前端CSS Transform 与动画核心知识点及蓝桥杯 Web 应用开发考点解析(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·css·职场和发展·蓝桥杯·html
问好眼6 小时前
《算法竞赛进阶指南》0x05 排序-1.电影
c++·算法·排序·信息学奥赛
CoderCodingNo6 小时前
【GESP】C++八级考试大纲知识点梳理 (6) 图论算法:最小生成树与最短路
c++·算法·图论
DeepModel6 小时前
【特征选择】嵌入法(Embedded)
人工智能·python·深度学习·算法
今儿敲了吗7 小时前
算法复盘——前缀和
笔记·学习·算法
ulias2127 小时前
智能指针简述
开发语言·c++·算法
寻寻觅觅☆7 小时前
东华OJ-基础题-58-素数表(C++)
开发语言·c++·算法