随笔分类 - 二分查找
摘要:题目:You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the qualit...
阅读全文
摘要:题目:Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exis...
阅读全文
摘要:思路:其实就是用普通的二分查找,只不过在找不到该数时,left下标就是应该插入的位置,返回该下标即可。Java代码如下: public int searchInsert(int[] nums, int target) { int left = 0; int righ...
阅读全文
摘要:思路:就是二分查找,只是查找一个数的范围,思路和二分查找相似,只是在mid查找到此数后,利用right-- 或 left++ 寻找该数的右边界和左边界。Java代码如下: public int[] searchRange(int[] nums, int target) { int...
阅读全文
摘要:思路:像题目中的例子,则结果返回数组元素0。和普通的二分查找不同的是,该二分查找的循环条件为(l num[r])。Java代码如下: public int findMin(int[] num) { int l = 0, r = num.length-1; whil...
阅读全文
摘要:/** * * 二分查找算法 * * * * @param srcArray 有序数组 * * @param target 查找元素 * * @return srcArray数组下标,没找到返回-1 */ public ...
阅读全文
浙公网安备 33010602011771号