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

相关推荐
晚风醉蝶35 分钟前
1-16-计数排序-CountingSort
python·算法·排序算法
疯狂打码的少年41 分钟前
【数据结构】排序算法:归并排序与基数排序
数据结构·笔记·算法·排序算法
ychqsq43 分钟前
122.收网
经验分享·职场和发展
fb_123451 小时前
Shell 脚本从 0 到精通|脚本规范 + 变量 + 数值运算 + 条件测试(可直接复制,面试必备)
chrome·面试·职场和发展
吃旺旺雪饼的小男孩1 小时前
U-Net 语义分割详解:原论文、PyTorch 实现与真实消融实
人工智能·pytorch·python·算法
lucas_AI1 小时前
35 种开源文档抽取配置,只有 4 个跨过 F1 0.5
人工智能·算法·llm
YSoup1 小时前
2026 安卓面试助手APP(安卓八股、题库)
android·面试·职场和发展
练习时长一年的RL研究者1 小时前
与AI对话后对 Actor-Critic 中 TD Target 的 a‘ 来源总结
算法
大熊背2 小时前
ISP图像处理中大数乘法溢出处理(二)
算法·大数乘法
roman_日积跬步-终至千里2 小时前
【算法3】二叉树中的最大路径和
算法