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;
    }
};
相关推荐
近津薪荼4 分钟前
递归专题(2)——合并链表
c++·学习·算法·链表
Asher阿舍技术站5 分钟前
【数字通信理论系列】四、载波相位同步
算法·载波同步
-Try hard-9 分钟前
队列 | 二叉树
算法
Sagittarius_A*11 分钟前
灰度变换与阈值化:从像素映射到图像二值化的核心操作【计算机视觉】
图像处理·人工智能·opencv·算法·计算机视觉·图像阈值·灰度变换
Nie_Xun20 分钟前
卡尔曼滤波(EKF/IEKF)与非线性优化(高斯-牛顿法)的统一关系
算法
仰泳的熊猫35 分钟前
题目1433:蓝桥杯2013年第四届真题-危险系数
数据结构·c++·算法·蓝桥杯·深度优先·图论
平哥努力学习ing36 分钟前
补充 part 1——防御性编程
算法
wbs_scy1 小时前
C++:智能指针完全指南(原理、用法与避坑实战,从 RAII 到循环引用)
开发语言·c++·算法
u0109272711 小时前
C++中的对象池模式
开发语言·c++·算法
试试勇气1 小时前
算法工具箱之哈希表
数据结构·算法·散列表