435. 无重叠区间
class Solution {
public:
static bool cmp(vector<int>&a,vector<int>&b)
{
if(a[0]==b[0]) return a[1]<b[1];
return a[0]<b[0];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),cmp);
int left=intervals[0][0];
int right=intervals[0][1];
int count=0;
for(int i=1;i<intervals.size();i++)
{
if(intervals[i][0]<right)
{
count++;
left=min(left,intervals[i][0]);
right=min(right,intervals[i][1]);
}
else
{
left=min(left,intervals[i][0]);
right=max(right,intervals[i][1]);
}
}
return count;
}
};
763. 划分字母区间
class Solution {
public:
vector<int> partitionLabels(string s) {
unordered_map<char,int> umap;
for(int i=0;i<s.size();i++)
{
umap[s[i]]++;
}
unordered_set<char> uset;
vector<int> res;
int count=0;
int pre=0;
for(int i=0;i<s.size();i++)
{
uset.insert(s[i]);
cout<<uset.size()<<endl;
umap[s[i]]--;
if(umap[s[i]]==0)
{
count++;
cout<<"count"<<count<<endl;
if(count==uset.size())
{
res.push_back(i+1-pre);
pre+=i+1-pre;
count=0;
uset.clear();
}
}
}
return res;
}
};
56. 合并区间
class Solution {
public:
static bool cmp(vector<int>&a,vector<int>&b)
{
if(a[0]==b[0]) return a[1]<b[1];
return a[0]<b[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),cmp);
vector<vector<int>> res;
int left=intervals[0][0];
int right=intervals[0][1];
if(intervals.size()==1) return intervals;
for(int i=1;i<intervals.size();i++)
{
if(intervals[i][0]<=right)
{
left=min(left,intervals[i][0]);
right=max(right,intervals[i][1]);
}
else
{
res.push_back(vector<int> {left,right});
left=intervals[i][0];
right=intervals[i][1];
}
if(i==intervals.size()-1)
res.push_back(vector<int> {left,right});
}
return res;
}
};
文章来源地址https://www.toymoban.com/news/detail-658125.html
文章来源:https://www.toymoban.com/news/detail-658125.html
到了这里,关于代码随想录打卡第35天的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!