文章链接:代码随想录
题目链接:435. 无重叠区间
依旧是区间问题
class Solution {
public:
static bool cmp(vector<int>& a, vector<int>& b){
return a[0] < b[0];
}
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end(), cmp);
int result = 0;
for (int i = 1; i < intervals.size(); i++){
if (intervals[i][0] < intervals[i - 1][1]){
intervals[i][1] = min(intervals[i][1], intervals[i - 1][1]);
result++;
}
}
return result;
}
};
文章链接:代码随想录
题目链接:763.划分字母区间
注意是更新到这段字符的最远边界,而不是当这个字符,所以要用
r = max(hash[s[i] - 'a'], r);
class Solution {
public:
vector<int> partitionLabels(string s) {
int hash[26] = {0};
for (int i = 0; i < s.size(); i++){
hash[s[i] - 'a'] = i;
}
vector<int> res;
int l = 0, r = 0;
for (int i = 0; i < s.size(); i++){
r = max(hash[s[i] - 'a'], r);
if (i == r){
res.push_back(r - l + 1);
l = i + 1;
}
}
return res;
}
};
注意直接用res.back()比较更新的操作
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<vector<int>> res;
res.push_back(intervals[0]);
for (int i = 1; i < intervals.size(); i++){
if (intervals[i][0] <= res.back()[1]){
res.back()[1] = max(intervals[i][1], res.back()[1]);
}
else{
res.push_back(intervals[i]);
}
}
return res;
}
};
第三十六天打卡,争取5号之前过一遍操作系统和计网,加油!!!