当前位置: 首页 > article >正文

27.贪心算法5

合并区间

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;
    }
};

和分割字符串那个一样

单调递增的数字

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;
    }
};

监控二叉树

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;
    }
};

http://www.kler.cn/a/565014.html

相关文章:

  • uni小程序wx.switchTab有时候跳转错误tab问题,解决办法
  • BladeX框架接口请求跨域
  • Java面试题总结 - Java集合篇(附答案)
  • 【Linux】修改 core 文件大小和路径
  • 微服务测试
  • Geo3D城市引擎大规模建筑植被渲染
  • AppInventor2 vs Android Studio
  • 基于Python socket库构建的基于 P2P 的文件共享系统示例
  • STM32 物联网智能家居 (七) 设备子系统--风扇控制
  • dockerfile基于alpine构建haproxy
  • 突破加速度计的精度与量程瓶颈:HEROS-GAN技术
  • drupal可以自动将测试环境的网页部署到生产环境吗
  • 蓝桥杯 Java B 组之最短路径算法(Dijkstra、Floyd-Warshall)
  • 【vue3+highCharts】图表及字体大小自适应
  • 【动态规划篇】正则表达式与通配符:开启代码匹配的赛博奇幻之旅
  • 大数据SQL调优专题——底层调优
  • 详解linuxC编程下的同步原语
  • Text2SQL数据集和技术方案整理
  • Python基于机器学习的微博舆情情感分析系统,微博评论情感分析可视化系统(全新升级)
  • Python----数据分析(Numpy四:广播机制,数组的运算,统计计算,where函数)