题解 | #不重叠的身高#
不重叠的身高
https://www.nowcoder.com/practice/b11cb8804f5d4eb3b221a019efe3ad5f
先将区间排序,随后合并
#include <climits> #include <vector> bool cmp(vector<int> a, vector<int> b){ return a[0]< b[0]; } class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param intervals int整型vector<vector<>> * @return int整型vector<vector<>> */ vector<vector<int>> mergeAnimalRanges(vector<vector<int> >& intervals) { // write code here sort(intervals.begin(), intervals.end(), cmp); int left = INT_MIN, right = INT_MAX; vector<vector<int>> ret; for(int i=0;i<intervals.size();i++){ if(left==INT_MIN){ left = intervals[i][0]; right = intervals[i][1]; } else if(intervals[i][0]<=right){ right = max(intervals[i][1], right); } else { ret.push_back({left, right}); left = intervals[i][0]; right = intervals[i][1]; } } if(left!=INT_MIN){ ret.push_back({left, right}); } return ret; } };