温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Lintcode14 First Position of Target Solution 题解

发布时间:2020-07-21 13:33:28 来源:网络 阅读:287 作者:coderer 栏目:软件技术

【题目描述】

For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.

If the target number does not exist in the array, return -1.

给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。

【题目链接】

http://www.lintcode.com/en/problem/first-position-of-target/

【题目解析】

这题目要求O(logn)的复杂度,又是sorted array,话不多说我们来搞二分法。注意要求first position of target所以当(array[mid] == target)的时候,操作是end = mid。

二分查找到第一个target后使用逐个向前查询第一个target

全程使用二分查找

【答案链接】

http://www.jiuzhang.com/solutions/first-position-of-target/


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI