27.贪心算法5

ops/2025/3/4 22:13:00/

合并区间

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.ppmy.cn/ops/163142.html

相关文章

腾讯云AI代码助手评测:如何智能高效完成Go语言Web项目开发

腾讯云AI代码助手评测&#xff1a;如何智能高效完成Go语言Web项目开发 ?? 文章目录 腾讯云AI代码助手评测&#xff1a;如何智能高效完成Go语言Web项目开发 ?? 背景引言开发环境介绍腾讯云AI代码助手使用实例 1. 代码补全2. 技术对话3. 代码优化4. 规范代码5. Bug处理 获得…

第七章:项目实战 - 第三节 - Tailwind CSS 电商网站开发

本节将介绍如何使用 Tailwind CSS 开发一个现代化的电商网站&#xff0c;包括商品展示、购物车、结算流程等核心功能的实现。 商品列表 商品卡片组件 // components/ProductCard.tsx interface ProductCardProps {product: {id: string;title: string;price: number;image: …

大白话html 第四章学习图像和多媒体标签

大白话html 第四章学习图像和多媒体标签 图像标签 <img> 想象一下你要在网页上展示一张漂亮的照片&#xff0c;这时候就需要用到 <img> 标签。它就像是一个相框&#xff0c;能把你指定的图片“装”到网页里。 关键属性&#xff1a; src&#xff1a;这个属性就像…

【洛谷贪心算法】P1106删数问题

这道题可以使用贪心算法来解决&#xff0c;核心思路是尽量让高位的数字尽可能小。当我们逐步删除数字时&#xff0c;会优先删除高位中相对较大的数字。具体做法是从左到右遍历数字序列&#xff0c;当发现当前数字比它后面的数字大时&#xff0c;就删除当前数字&#xff0c;直到…

如何防止Python网络爬虫爬取网站内容

要防止Python网络爬虫爬取网站内容&#xff0c;可以从以下几个方面入手&#xff1a; 遵守Robots.txt文件&#xff1a;首先&#xff0c;网站管理员可以通过robots.txt文件明确告知爬虫哪些页面可以抓取&#xff0c;哪些不可以。爬虫在抓取之前应先检查该文件&#xff0c;尊重网站…

图论-腐烂的橘子

994.腐烂的橘子 在给定的 m x n 网格 grid 中&#xff0c;每个单元格可以有以下三个值之一&#xff1a;值 0 代表空单元格&#xff1b; 值 1 代表新鲜橘子&#xff1b; 值 2 代表腐烂的橘子。 每分钟&#xff0c;腐烂的橘子 周围 4 个方向上相邻 的新鲜橘子都会腐烂。返回 直到…

RA-Eco-RA2L1-48PIN-V1.0开发板RTC时钟

前言 本文将详细介绍如何在e2studio开发环境中为RA2L1&#xff08;48引脚版本&#xff09;配置RTC&#xff08;Real-Time Clock&#xff0c;实时时钟&#xff09;模块&#xff0c;设置时钟日历&#xff0c;并通过1秒周期中断触发串口打印当前时间。这对于需要实时时间显示的应…

游戏引擎学习第128天

开始 然而&#xff0c;我们仍然有一些工作要做&#xff0c;渲染部分并没有完全完成。虽然现在已经能够运行游戏&#xff0c;而且帧率已经可以接受&#xff0c;但仍然有一些东西需要进一步完善。正在使用调试构建编译版本&#xff0c;虽然调试版本的性能不如优化版本&#xff0…