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. Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)

    QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...

  2. SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法

    今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...

  3. Android学习笔记——MixLayout

    该工程的功能是实现LinearLayout+TableLayout 以下代码是MainActivity.java中的代码 package com.example.mixlayout; import a ...

  4. docker快速启动脚本

    #!/bin/sh PID=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $PID -u -i -n -p n ...

  5. VBO, VAO, Generic Vertex Attribute

    VBO - 用于存储顶点数据的Buffer Object. VAO - 用于组织VBO的对象. Generic Vertex Attribute - 通用顶点属性. For example, the ...

  6. #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0

    头文件处理 #import <UIKit/UIKit.h> #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 #else #imp ...

  7. XtraFinder在OSX10.11的使用

    重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...

  8. Fiddler源代码分享

    frmViewer.cs: namespace Fiddler{    using Microsoft.Win32;    using System;    using System.Collecti ...

  9. Chrome插件开发

    参考文档: http://open.chrome.360.cn/extension_dev/overview.html 参考博文: http://www.cnblogs.com/mfryf/p/370 ...

  10. 【转】 C++使用zlib库(-)

    来自:  http://blog.chinaunix.net/uid-24607609-id-2118143.html   今天看到一个gzopen函数,搜了一下他的系列函数,及相关用法   C++使 ...