摘要: 粗暴方法,先把所有区间按照左边界的大小排序,然后在一个个遍历,如果有重叠部分的就重叠起来,如果没有的就将维护的res-temp变量加入结果数组中,最费劲的是两个区间之间的关系没有搞清楚,花了一些时间,贴代码。 1 bool cmp(vector<int>& a,vector<int>& b) 2 { 阅读全文
posted @ 2021-08-25 19:19 zhaohhhh 阅读(32) 评论(0) 推荐(0)
摘要: 二分查找,写起来比较流畅,唯一的小问题是mid在数组中对应的值等于target时,需要将r和l都置为mid,方便之后的操作,贴代码 1 class Solution { 2 public: 3 vector<int> searchRange(vector<int>& nums, int target 阅读全文
posted @ 2021-08-25 17:19 zhaohhhh 阅读(34) 评论(0) 推荐(0)
摘要: 这道题无愧于描述里面的脑筋急转弯词条。用的是二分法,本质就是往高处走,峰值一定在高的地方,因为两边都是负无穷,所有只要有一个数比周边的数更大,顺着这个数就能找到一个峰值。贴代码 1 class Solution { 2 public: 3 int findPeakElement(vector<int 阅读全文
posted @ 2021-08-25 16:49 zhaohhhh 阅读(33) 评论(0) 推荐(0)
摘要: 小顶堆,三分钟写完,贴代码 class Solution { public: static bool cmp(int& a,int& b) { return a>b; } int findKthLargest(vector<int>& nums, int k) { priority_queue<in 阅读全文
posted @ 2021-08-25 10:09 zhaohhhh 阅读(23) 评论(0) 推荐(0)