文档讲解:代码随想录 (programmercarl.com)
视频讲解:代码随想录的个人空间-代码随想录个人主页-哔哩哔哩视频 (bilibili.com)
LeetCode 435. 无重叠区间
题目链接:435. 无重叠区间 - 力扣(LeetCode)
代码如下:
class Solution {
public:static bool cmp(vector<int>&a,vector<int>&b){return a[0]<b[0];}int eraseOverlapIntervals(vector<vector<int>>& intervals) {int count=1;sort(intervals.begin(),intervals.end(),cmp);for(int i=1;i<intervals.size();i++){if(intervals[i-1][1]<=intervals[i][0])count++;else intervals[i][1]=min(intervals[i-1][1],intervals[i][1]);}return intervals.size()-count;}
};
LeetCode 763.划分字母区间
题目链接:763. 划分字母区间 - 力扣(LeetCode)
代码如下
class Solution {
public:vector<int> partitionLabels(string s) {vector<int>res;int left=0;int right=0;int hash[26];for(int i=0;i<s.size();i++)hash[s[i]-'a']=i;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;}
};
LeetCode 56. 合并区间
题目链接:56. 合并区间 - 力扣(LeetCode)
解题代码如下:
class Solution {
public:static bool cmp(vector<int>&a,vector<int>&b){return a[0]<b[0];}vector<vector<int>> merge(vector<vector<int>>& intervals) {int left=0;int right=0;vector<vector<int>>res;sort(intervals.begin(),intervals.end(),cmp);// sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b){return a[0] < b[0];});res.push_back(intervals[0]);for(int i=1;i<intervals.size();i++){if(res.back()[1]<intervals[i][0])res.push_back(intervals[i]);else{res.back()[1]=max(res.back()[1],intervals[i][1]);}}return res;}
};