赞
踩
以下四个题都是重叠区间问题
class Solution {
public:
int findMinArrowShots(vector<vector<int>>& points) {
sort(points.begin(), points.end());
int ret = 1;
for (int i = 1; i < points.size(); i++) {
if (points[i][0] > points[i - 1][1]) ret++;
else points[i][1] = min(points[i - 1][1], points[i][1]);
}
return ret;
}
};
与上一个题极其相似,首先按照左边界排序,当重叠的时候,舍弃重叠的右边长的那个区间(即将右边界定为小的那个),ret++记录重叠区间个数。
class Solution {
public:
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
sort(intervals.begin(), intervals.end());
int ret = 0;
for (int i = 1; i < intervals.size(); i++) {
if (intervals[i][0] < intervals[i - 1][1]) {
ret++;
intervals[i][1] = min(intervals[i][1], intervals[i - 1][1]);
}
}
return ret;
}
};
class Solution { public: vector<int> partitionLabels(string s) { int hash[27] = {0}; for (int i = 0; i < s.size(); i++) { hash[s[i] - 'a'] = i; } vector<int> ret; int left = 0, right = 0; for (int i = 0; i < s.size(); i++) { right = max(hash[s[i] - 'a'], right); if (right == i) { ret.push_back(right - left + 1); left = i + 1; } } return ret; } };
和上面的435差不多,先按照左边界排序好,将第一组数据添加到ret中,之后如果满足后一个的左边界小于等于这个的右边界时候,更新ret中的这个(ret.back()[1]更新成大的右边界),不满足就把下一个添加进来,for循环是从i=1开始
class Solution { public: vector<vector<int>> merge(vector<vector<int>>& intervals) { sort(intervals.begin(), intervals.end()); if (intervals.size() == 1) return intervals; vector<vector<int>> ret; ret.push_back(intervals[0]); for (int i = 1; i < intervals.size(); i++) { if (intervals[i][0] <= ret.back()[1]) { ret.back()[1] = max(ret.back()[1], intervals[i][1]); } else ret.push_back(intervals[i]); } return ret; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。