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

相关推荐
高山上有一只小老虎18 小时前
字符串字符匹配
java·算法
愚润求学19 小时前
【动态规划】专题完结,题单汇总
算法·leetcode·动态规划
林太白19 小时前
跟着TRAE SOLO学习两大搜索
前端·算法
ghie909019 小时前
图像去雾算法详解与MATLAB实现
开发语言·算法·matlab
云泽80819 小时前
从三路快排到内省排序:探索工业级排序算法的演进
算法·排序算法
weixin_4684668520 小时前
遗传算法求解TSP旅行商问题python代码实战
python·算法·算法优化·遗传算法·旅行商问题·智能优化·np问题
·白小白20 小时前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode
FMRbpm20 小时前
链表5--------删除
数据结构·c++·算法·链表·新手入门
程序员buddha20 小时前
C语言操作符详解
java·c语言·算法
John_Rey21 小时前
API 设计哲学:构建健壮、易用且符合惯用语的 Rust 库
网络·算法·rust