LeetCode之二分查找
黄焖鸡米饭啊
嗯。
展开
-
Sqrt(x)
题目描述: Implement int sqrt(int x). Compute(计算) and return the square root of x. 思路:用二分查找搜寻其平方根,若其平方根不为整数,则要找到最后一个平方小于该值的数作为答案。 public class Sqrt_x { public static int mySqrt(int x) {原创 2017-08-06 19:49:08 · 319 阅读 · 0 评论 -
Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not f转载 2017-07-09 10:57:57 · 210 阅读 · 0 评论 -
寻找旋转排序数组中的最小值
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组[0,1,2,4,5,6,7] 可能变为[4,5,6,7,0,1,2])。 请找出其中最小的元素。 你可以假设数组中不存在重复元素。 示例 1: 输入: [3,4,5,1,2] 输出: 1 示例 2: 输入: [4,5,6,7,0,1,2] 输出: 0 思路:根据数组的特点使用二分查找,先将l指向n...原创 2019-09-01 15:05:14 · 143 阅读 · 0 评论 -
搜索插入位置
题目: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 你可以假设数组中无重复元素。 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例2: 输入: [1,3,5,6], 2 输出: 1 示例 3: 输入: [1,3,5,6], 7 输出: 4 示例 4: 输入: [1,3,5,6], 0 输出:...原创 2019-09-11 12:03:19 · 120 阅读 · 0 评论