随笔分类 -  Week1 二分专题

摘要:1 class Solution 2 { 3 public: 4 int hIndex(vector<int>& nums) 5 { 6 int l = 0,r = nums.size(); 7 while(l < r) 8 { 9 int mid = l + r + 1 >> 1; 10 11 / 阅读全文
posted @ 2020-03-21 21:42 Jinxiaobo0509 阅读(97) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int findPeakElement(vector<int>& nums) 5 { 6 int l = 0,r = nums.size() - 1; 7 while(l < r) 8 { 9 int mid = l + r >> 1 阅读全文
posted @ 2020-03-21 20:30 Jinxiaobo0509 阅读(121) 评论(0) 推荐(0)
摘要:1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int version); 3 4 class Solution 5 { 6 public: 7 int firstBadVersion(int n) 8 { 9 in 阅读全文
posted @ 2020-03-21 20:02 Jinxiaobo0509 阅读(112) 评论(0) 推荐(0)
摘要:1 //利用模板 2 3 //以最后一个值来划分 4 //如果比它小,在右边,更新r 5 //反之,更新l 6 class Solution 7 { 8 public: 9 int findMin(vector<int>& nums) 10 { 11 int l = 0,r = nums.size( 阅读全文
posted @ 2020-03-21 19:25 Jinxiaobo0509 阅读(127) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 bool searchMatrix(vector<vector<int>>& matrix, int target) 5 { 6 if(matrix.empty() || matrix[0].empty()) return false 阅读全文
posted @ 2020-03-21 18:24 Jinxiaobo0509 阅读(149) 评论(0) 推荐(0)
摘要:1 使用数组中的值作为索引下标进行遍历,遍历的结果肯定是一个环(有一个重复元素) 2 检测重复元素问题转换成检测环的入口 3 为了找到环的入口,可以进行如下步骤: 4 5 设置两个快慢指针, fast每次走两步,slow每次走一步,最终走了slow走了n步与fast相遇,fast走了2*n,fast 阅读全文
posted @ 2020-03-21 17:05 Jinxiaobo0509 阅读(139) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int mySqrt(int x) 5 { 6 double l = 0; 7 double r = x; 8 9 while(r - l > 1e-6) 10 { 11 double mid = (l + r)/2; 12 if(m 阅读全文
posted @ 2020-03-20 16:22 Jinxiaobo0509 阅读(133) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 int searchInsert(vector<int>& nums, int target) 5 { 6 int n = nums.size(); 7 int l = 0; 8 int r = n - 1; 9 if(target 阅读全文
posted @ 2020-03-17 18:33 Jinxiaobo0509 阅读(370) 评论(0) 推荐(0)
摘要:1 class Solution 2 { 3 public: 4 vector<int> searchRange(vector<int>& nums, int target) 5 { 6 auto it = find(nums.begin(),nums.end(),target); 7 if(it 阅读全文
posted @ 2020-03-17 18:20 Jinxiaobo0509 阅读(110) 评论(0) 推荐(0)
摘要:一、如果左边有序 1、target小于中点的值且小于起点的值:l = mid + 1 2、target小于中点的值且大于等于起点的值:r = mid - 1 3、target大于中点的值:l = mid + 1 二、如果右边有序 1、target大于中点的值且小于等于终点的值:l = mid + 1 阅读全文
posted @ 2020-03-17 17:58 Jinxiaobo0509 阅读(113) 评论(0) 推荐(0)