ITPub博客

首页 > IT职业 > IT生活 > Lintcode14 First Position of Target Solution 题解

Lintcode14 First Position of Target Solution 题解

IT生活 作者:牛客001 时间:2017-08-02 20:39:25 0 删除 编辑
【题目描述】 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/

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31478388/viewspace-2142994/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2017-07-25

  • 博文量
    13
  • 访问量
    3948