27.贪心算法5

合并区间

cpp 复制代码
class Solution {
public:
    static bool cmp(const vector<int> & a,const vector<int> & b){
        return a[0]<b[0];
    }
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(),intervals.end(),cmp);
        vector<int> tmp=intervals[0];
        vector<vector<int>> res;
        int n=intervals.size();
        for(int i=1;i<n;i++){
            if(tmp[1]>=intervals[i][0]){
                tmp[1]=tmp[1]>intervals[i][1]?tmp[1]:intervals[i][1];
            }else{
                res.push_back(tmp);
                tmp=intervals[i];
            }
        }
        res.push_back(tmp);
        return res;
    }
};

和分割字符串那个一样

单调递增的数字

cpp 复制代码
class Solution {
public:
    int monotoneIncreasingDigits(int k) {
        string s=to_string(k);
        int index=-1;
        int n=s.size();
        for(int i=0;i<n-1;i++){
            if(s[i]>s[i+1]){
                s[i]--;
                index=i+1;
                while(index>1&&s[index-1]<s[index-2]){
                    index--;
                    s[index-1]--;
                }
                break;
            }
        }
        for(int i=index;i>=0&&i<n;i++){
            s[i]='9';
        }
        int res=stoi(s);
        return res;
    }
};

监控二叉树

cpp 复制代码
class Solution {
public:
    
    int minCameraCover(TreeNode* root) {
    unordered_map<TreeNode* ,int> mymap;
    stack<TreeNode*> sta;
    sta.push(root);
    TreeNode* cur=root;
    int count=0;
    while(!sta.empty()){
        cur=sta.top();
        sta.pop();
        if(cur){
            sta.push(cur);
            sta.push(nullptr);
            if(cur->left){
                sta.push(cur->left);
            }
            if(cur->right){
                sta.push(cur->right);
            }
        }else{
            cur=sta.top();
            sta.pop();
            bool put=0;
            if(cur->right){
                if(mymap[cur->right]==0){
                    put=1;
                }else if(mymap[cur->right]==2){
                    mymap[cur]=1;
                }
            }
            if(cur->left){
                if(mymap[cur->left]==0){
                    put=1;
                }else if(mymap[cur->left]==2){
                    mymap[cur]=1;
                }
            }
            if(put){
                mymap[cur]=2;
                count++;
                mymap[cur->left]=mymap[cur->right]=1;
            }else if(cur==root&&!mymap[cur]){
                mymap[cur]=2;
                count++;
            }
        }
    }    
        return count;
    }
};
相关推荐
Evand J7 小时前
【MATLAB例程,图像滤波5】 反谐波均值滑动窗口滤波(CHMF)图像降噪与质量评价,附代码下载链接
图像处理·算法·计算机视觉·matlab·均值算法·滑动窗口滤波·均值滑动
血小板要健康7 小时前
链表 阶段算法总结
java·数据结构·笔记·算法·leetcode·链表
a187927218317 小时前
【算法】回溯算法(三):三记重锤与 N 皇后——记忆化、状态设计与三层漏斗
算法·leetcode·go·剪枝·回溯·n皇后·算法讲解
HugoStudio_SWAN9 小时前
洛谷 P1420 / P1179 / B4262 最长连号、数字统计与词频统计——统计的三种面孔
c++·学习·程序人生·算法
青 春 记 忆10 小时前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
阳明山水11 小时前
销量预测2025—2026:从模型竞赛到系统融合的范式转型
人工智能·深度学习·算法·机器学习·架构
桐盛科技11 小时前
拒绝“漂绿”风险:基于边缘计算的碳排放数据清洗算法与实时校验逻辑
人工智能·算法·边缘计算
Tongzhi202611 小时前
通芝科技考勤软件三大发展趋势:AI大模型、无感识别与数据底座
大数据·科技·算法·爬山算法
eBest数字化转型方案12 小时前
Route Optimization for FMCG:多目标排线算法的工程实现
大数据·人工智能·算法
residual_fan12 小时前
持续对比强化学习(Continual Contrastive Reinforcement Learning)论文分享
人工智能·算法·数据挖掘·数据分析