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.

Example

If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.

分析:
题中给出一个有序数组,每个元素不一定唯一,找到第一次出现target的位置。
这道题处理的关键点就是当nums[mid] == target的时候,不是将mid返回,
而是把end移动的mid点,继续向前查找看有无相同的元素。
class Solution {
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
public int binarySearch(int[] nums, int target) {
if (nums.length == 0) {
return -1;
}
int start = 0, end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
}else if (nums[mid] < target) {
start = mid;
}else {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
}

[lintcode 14] First Position of Target的更多相关文章

  1. 14. First Position of Target 【easy】

    14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...

  2. LintCode First Position of Target

    找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...

  3. Lintcode: First Position of Target (Binary Search)

    Binary search is a famous question in algorithm. For a given sorted array (ascending order) and a ta ...

  4. First Position of Target

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

  5. Last Position of Target

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

  6. LintCode Search Insert Position

    找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * para ...

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. 2 - Binary Search & LogN Algorithm

    254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...

  9. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

随机推荐

  1. jquery selector 基础

    转自:http://www.cnblogs.com/zwl12549/archive/2008/08/09/1264163.html query的这套选择符是比较帅气的,借用了XPath2.0和CSS ...

  2. BZOJ3226: [Sdoi2008]校门外的区间

    感觉很有趣的题呢. 每个点拆成两个,线段树维护. 不过这题难点其实在输入输出. #include<bits/stdc++.h> #define N (1<<17) #defin ...

  3. IIS------无法打开登录所请求的数据库 "company"。登录失败。 用户 'IIS APPPOOL\AppPool 4.0' 登录失败。

    链接: http://www.cnblogs.com/VortexPiggy/archive/2013/04/06/3002055.html

  4. JDK的安装和配置

    JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows xp下安装有点问题,所以在WIN7下安装 WIN7操作系统有32位和64位,分别要下载对应的J ...

  5. Java实例分析:宠物商店

    设计一个“宠物商店”,在宠物商店中可以有多种宠物,试表示出此种关系,并要求可以根据宠物的关键字查找相应的宠物信息. //======================================== ...

  6. Google Guava14.0 瓜娃学习笔记

    Guava 是java api的增强与扩展,提供复杂的java 数据结构,使你的代码更简短精炼,具有良好的可读性.看看guava给我们提供了哪些很酷的功能: 集合创建: Map<String, ...

  7. OC-self关键字

    self关键字 1. 成员变量和局部变量同名 当成员变量和局部变量同名时,采取就近原则,访问的是局部变量 用self访问成员变量,区分同名的局部变量 2.使用细节 1)     出现的地方:所有的OC ...

  8. fis总结

    1.fis捕获组 $1.$2.$3……是正则表达式替换中对捕获组的引用 $0或$&是对整个匹配字符串的引用 2.fis命令 fis3 release -d ../output prod -wL ...

  9. Java并发编程核心方法与框架-TheadPoolExecutor的使用

    类ThreadPoolExecutor最常使用的构造方法是 ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAli ...

  10. Java并发编程核心方法与框架-CountDownLatch的使用

    Java多线程编程中经常会碰到这样一种场景:某个线程需要等待一个或多个线程操作结束(或达到某种状态)才开始执行.比如裁判员需要等待运动员准备好后才发送开始指令,运动员要等裁判员发送开始指令后才开始比赛 ...