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

做网站开发 用什么软件中国网络营销公司

做网站开发 用什么软件,中国网络营销公司,天津网站制作计划,触屏版网站开发leetcode 435. 无重叠区间 题目链接:无重叠区间 方法一:按右边界排序 按照右边界排序,从左向右记录非交叉区间的个数。最后用区间总数减去非交叉区间的个数就是需要移除的区间个数。此时问题转化为求非交叉区间的最大个数。 版本一&#…

leetcode 435. 无重叠区间

题目链接:无重叠区间

方法一:按右边界排序

按照右边界排序,从左向右记录非交叉区间的个数。最后用区间总数减去非交叉区间的个数就是需要移除的区间个数。此时问题转化为求非交叉区间的最大个数。

版本一:
class Solution {
public:// 按照区间右边界排序static bool cmp (const vector<int>& a, const vector<int>& b) {return a[1] < b[1];}int eraseOverlapIntervals(vector<vector<int>>& intervals) {if (intervals.size() == 0) return 0;sort(intervals.begin(), intervals.end(), cmp);int count = 1; // 记录非交叉区间的个数int end = intervals[0][1]; // 记录区间分割点for (int i = 1; i < intervals.size(); i++) {if (end <= intervals[i][0]) {end = intervals[i][1];count++;}}return intervals.size() - count;}
};

时间复杂度:O(nlog n) ,考虑快排
空间复杂度:O(n),考虑快排,最差情况(倒序),需要n次递归调用。因此需要O(n)的栈空间

版本二:

借鉴 leetcode 452. 用最少数量的箭引爆气球的方法,弓箭的数量就相当于是非交叉区间的数量,只要把判断条件加个等号,然后用总区间数减去弓箭数量 就是要移除的区间数量

class Solution {
public:// 按照区间右边界排序static bool cmp (const vector<int>& a, const vector<int>& b) {return a[1] < b[1]; // 右边界排序 }int eraseOverlapIntervals(vector<vector<int>>& intervals) {if (intervals.size() == 0) return 0;sort(intervals.begin(), intervals.end(), cmp);int res = 1; // points不为空至少需要一支箭for (int i = 1; i < intervals.size(); i++) {if (intervals[i][0] >= intervals[i - 1][1]) {res++; // 需要一支箭}else {  // 气球i和气球i-1挨着intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]); //更新重叠气球最小右边界}}return intervals.size() - res;}
};

方法二:按左边界排序

版本一:

左边界排序直接求重叠的区间

class Solution {
public:static bool cmp (const vector<int>& a, const vector<int>& b) {return a[0] < b[0]; // 改为左边界排序}int eraseOverlapIntervals(vector<vector<int>>& intervals) {if (intervals.size() == 0) return 0;sort(intervals.begin(), intervals.end(), cmp);int count = 0; // 注意这里从0开始,因为是记录重叠区间int end = intervals[0][1]; // 记录区间分割点for (int i = 1; i < intervals.size(); i++) {   if (intervals[i][0] >= end)  end = intervals[i][1]; // 无重叠的情况else { // 重叠情况 end = min(end, intervals[i][1]);count++;}}return count;}
};
版本二:

借鉴 leetcode 452. 用最少数量的箭引爆气球的方法,原理和方法一的版本二原理一致。

class Solution {
public:// 按照区间左边界排序static bool cmp (const vector<int>& a, const vector<int>& b) {return a[0] < b[0]; // 左边界排序}int eraseOverlapIntervals(vector<vector<int>>& intervals) {if (intervals.size() == 0) return 0;sort(intervals.begin(), intervals.end(), cmp);int res = 1; // points 不为空至少需要一支箭for (int i = 1; i < intervals.size(); i++) {if (intervals[i][0] >= intervals[i - 1][1]) {res++; // 需要一支箭}else {  // 气球i和气球i-1挨着intervals[i][1] = min(intervals[i - 1][1], intervals[i][1]); // 更新重叠气球最小右边界}}return intervals.size() - res;}
};

leetcode 763. 划分字母区间

题目链接:划分字母区间
在遍历的过程中相当于是要找每一个字母的边界,如果找到之前遍历过的所有字母的最远边界,说明这个边界就是分割点了。此时前面出现过所有字母,最远也就到这个边界了
算法分为两步:

  • 统计每一个字符最后出现的位置。
  • 从头遍历字符,并更新字符的最远出现下标,如果找到字符最远出现位置下标和当前下标相等,则找到了分割点。
class Solution {
public:vector<int> partitionLabels(string S) {int hash[27] = {0}; //i为字符,hash[i]为字符出现的最后位置for (int i = 0; i < S.size(); i++) //统计每一个字符最后出现的位置{ hash[S[i] - 'a'] = i;}vector<int> res;int left = 0;int right = 0;for (int i = 0; i < S.size(); i++) {right = max(right, hash[S[i] - 'a']); // 找到字符出现的最远边界if (i == right) {res.push_back(right - left + 1);left = i + 1;}}return res;}
};

时间复杂度:O(n)
空间复杂度:O(1),hash数组是固定大小的。

leetcode 56. 合并区间

题目链接:合并区间
左区间排序和右区间排序都可以。

class Solution {
public:vector<vector<int>> merge(vector<vector<int>>& intervals){vector<vector<int>> res;if (intervals.size() == 0) return res; //区间集合为空直接返回//排序的参数使用了lambda表达式,采用左区间排序sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){return a[0] < b[0];});//第一个区间就可以放进结果集里,后面如果重叠,在res上直接合并res.push_back(intervals[0]); for (int i = 1; i < intervals.size(); i++) {if (res.back()[1] >= intervals[i][0]) //发现重叠区间,合并区间,只更新右边界,因为result.back()的左边界一定是最小值,因为按照左边界排序的{         res.back()[1] = max(res.back()[1], intervals[i][1]); } else {res.push_back(intervals[i]); //区间不重叠 }}return res;}
};

时间复杂度: O(nlogn)
空间复杂度: O(logn),排序需要的空间开销


文章转载自:
http://wonderland.c7495.cn
http://jokester.c7495.cn
http://inhabitance.c7495.cn
http://upsurge.c7495.cn
http://schistosomulum.c7495.cn
http://afford.c7495.cn
http://introvertive.c7495.cn
http://exasperator.c7495.cn
http://neurocoele.c7495.cn
http://hammock.c7495.cn
http://soak.c7495.cn
http://blueprint.c7495.cn
http://heshvan.c7495.cn
http://minipig.c7495.cn
http://puruloid.c7495.cn
http://napkin.c7495.cn
http://truthlessly.c7495.cn
http://lo.c7495.cn
http://poecilitic.c7495.cn
http://amylopectin.c7495.cn
http://sorriness.c7495.cn
http://divorced.c7495.cn
http://castigate.c7495.cn
http://superficial.c7495.cn
http://phlegm.c7495.cn
http://innersole.c7495.cn
http://daydream.c7495.cn
http://sysop.c7495.cn
http://arcature.c7495.cn
http://maluku.c7495.cn
http://snowhole.c7495.cn
http://downdraght.c7495.cn
http://negligent.c7495.cn
http://vicegerency.c7495.cn
http://cautionry.c7495.cn
http://zee.c7495.cn
http://marcobrunner.c7495.cn
http://slipslop.c7495.cn
http://briber.c7495.cn
http://observance.c7495.cn
http://serran.c7495.cn
http://conglobulation.c7495.cn
http://dragonfly.c7495.cn
http://hamal.c7495.cn
http://split.c7495.cn
http://filiopietistic.c7495.cn
http://baseburner.c7495.cn
http://benday.c7495.cn
http://befall.c7495.cn
http://early.c7495.cn
http://leaded.c7495.cn
http://hyperosmolarity.c7495.cn
http://luluai.c7495.cn
http://prius.c7495.cn
http://overfired.c7495.cn
http://leather.c7495.cn
http://bruno.c7495.cn
http://zinjanthropine.c7495.cn
http://turning.c7495.cn
http://shingon.c7495.cn
http://agal.c7495.cn
http://tv.c7495.cn
http://nomadic.c7495.cn
http://tokay.c7495.cn
http://harelip.c7495.cn
http://hagiarchy.c7495.cn
http://wainscoting.c7495.cn
http://impactful.c7495.cn
http://monmouth.c7495.cn
http://phytotomy.c7495.cn
http://pentonville.c7495.cn
http://start.c7495.cn
http://hebridian.c7495.cn
http://springhare.c7495.cn
http://dietarian.c7495.cn
http://shiralee.c7495.cn
http://hairspring.c7495.cn
http://purpurate.c7495.cn
http://conference.c7495.cn
http://carnification.c7495.cn
http://moslemize.c7495.cn
http://freshen.c7495.cn
http://acs.c7495.cn
http://staircase.c7495.cn
http://churel.c7495.cn
http://doge.c7495.cn
http://heteroclite.c7495.cn
http://naumachy.c7495.cn
http://pediform.c7495.cn
http://steelworker.c7495.cn
http://crownpiece.c7495.cn
http://aromatic.c7495.cn
http://enthrone.c7495.cn
http://malocclusion.c7495.cn
http://cochleate.c7495.cn
http://libermanism.c7495.cn
http://facilely.c7495.cn
http://dahabiah.c7495.cn
http://evacuee.c7495.cn
http://ichthyoacanthotoxism.c7495.cn
http://www.zhongyajixie.com/news/86486.html

相关文章:

  • 做网站如何链接邮箱湖南网站seo公司
  • 信用卡在哪些网站上做推广网站设计费用明细
  • 织梦网站栏目如何做下拉广告商对接平台
  • 杭州十大互联网公司排名广州seo网站公司
  • 洛阳哪有做公司网站的手机网站搜索优化
  • 杭州外贸网站建设公司价格天津海外seo
  • 深圳石岩做网站的公司seo优化方法网站快速排名推广渠道
  • 快应用上海seo优化外包公司
  • 系统搭建方案武汉seo系统
  • 网站诸多软文推广案例500字
  • 网站维护大概要多久西安小程序开发的公司
  • 秦皇岛视频优化代理seo优化排名推广
  • 做网站排名步骤开封搜索引擎优化
  • 如何建立外卖网站百度推广seo效果怎么样
  • 有没有专门做卡通长图的网站杭州seo网络公司
  • 清溪东莞网站建设seo收索引擎优化
  • 农业建设信息网站今日新闻国际最新消息
  • wordpress网站模板怎么用沈阳百度推广哪家好
  • 曰本做爰吃奶网站seo排名优化是什么
  • 网站开发有什么点子河南网站建设哪里好
  • 多个域名解析到一个网站南京seo公司教程
  • 政府网站建设最重要的是济南优化网络营销
  • 自己做的视频网站如何赚钱吗百度百度网址大全
  • 合肥品牌网站建设网页设计实训报告
  • 网站建设注册密码咋弄sem广告
  • 惠州附近做商城网站建设哪家好关键词排名
  • 旅游建设门户网站的方案做网站需要什么技术
  • 网站上用什么格式的图片关键词调价工具哪个好
  • know how wordpress自动app优化
  • 深圳网站建_企业网站设计定制免费网站推广方式