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

相关推荐
_殊途1 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说6 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen6 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove6 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty7 小时前
排序算法(二):插入排序
算法·排序算法
然我7 小时前
面试官:如何判断元素是否出现过?我:三种哈希方法任你选
前端·javascript·算法
F_D_Z8 小时前
【EM算法】三硬币模型
算法·机器学习·概率论·em算法·极大似然估计
秋说8 小时前
【PTA数据结构 | C语言版】字符串插入操作(不限长)
c语言·数据结构·算法
凌肖战9 小时前
力扣网编程135题:分发糖果(贪心算法)
算法·leetcode