435. 无重叠区间

按照起点排序

  1. 若出现重叠,就将当前的终点取为当前终点和上一终点的最小值

    1. intervals[i][1] = min(intervals[i-1][1],intervals[i][1]);
  2. 若没出现重叠,

    1. class Solution {
    2. public:
    3. static bool cmp(vector<int>& a,vector<int>& b)
    4. {
    5. if(a[0]==b[0])return a[1]<b[1];
    6. return a[0]<b[0];
    7. }
    8. int eraseOverlapIntervals(vector<vector<int>>& intervals) {
    9. sort(intervals.begin(),intervals.end(),cmp);
    10. int num = 0;
    11. for(int i=1;i<intervals.size();i++)
    12. {
    13. if(intervals[i][0]<intervals[i-1][1])
    14. {
    15. intervals[i][1] = min(intervals[i-1][1],intervals[i][1]);
    16. intervals[i][0]=intervals[i-1][0];
    17. num++;
    18. }
    19. }
    20. return num;
    21. }
    22. };