![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二分查找
尘埃飞舞
凡心所向,素履以往;
生如逆旅,一苇以航。
展开
-
LeetCode-二分查找-35. 搜索插入位置
35. 搜索插入位置 思路:二分查找 class Solution { public: int searchInsert(vector<int>& nums, int target) { int low=0,high=nums.size()-1; while(low<=high){ int middle = low + (high-low)/2; if(nums[middle]==target)原创 2021-10-17 14:42:40 · 77 阅读 · 0 评论 -
LeetCode-二分查找-278. 第一个错误的版本
278. 第一个错误的版本 思路:二分查找法 // The API isBadVersion is defined for you. // bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int low = 0,high=n; while(low<=high){// 循环直至区间左右端点相同 int middle原创 2021-10-17 14:21:43 · 81 阅读 · 0 评论 -
LeetCode-二分查找-374. 猜数字大小
374. 猜数字大小 思路:二分法 /** * Forward declaration of guess API. * @param num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * int guess(int num);原创 2021-10-14 20:11:39 · 71 阅读 · 0 评论 -
LeetCode-数组-704. 二分查找
描述 704. 二分查找 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 示例 1: 输入: nums = [-1,0,3,5,9,12], target = 9 输出: 4 解释: 9 出现在 nums 中并且下标为 4 示例 2: 输入: nums = [-1,0,3,5,9,12], target = 2 输出: -1 解释: 2 不存在 nums 中因此返回 -1 思路一:原创 2021-08-22 16:48:02 · 80 阅读 · 0 评论 -
LeetCode-二分查找-69. Sqrt(x)
69. Sqrt(x) 思路:二分查找 class Solution { public: int mySqrt(int x) { //1:考虑特殊情况 if(x<2) return x; //2:初始化边界 int low = 2,high = x/2; //平方跟的最大在它的一半以内,缩短时间 while(low<=high) { int middle = low + (high原创 2021-10-14 19:41:59 · 81 阅读 · 0 评论