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.

分析

找排序数组某个数字的右边界
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Solution {
    /**
     * @param nums: An integer array sorted in ascending order
     * @param target: An integer
     * @return an integer
     */
    public int lastPosition(int[] nums, int target) {
        // Write your code here
        if(nums == null || nums.length == 0)
            return -1;
        int left = 0, right = nums.length -1, mid;
        while(left < right){
            mid = left + (right - left + 1) / 2;//insure when right is 1 bigger than left, mid eaqual to right 
            if(nums[mid] > target){
                right = mid - 1;
            }
            else{
                left = mid;
            }
        }
        if(nums[right] == target)
            return right;
        else
            return -1;
    }
}

Last Position of Target的更多相关文章

  1. [lintcode 14] First Position of Target

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

  2. LintCode First Position of Target

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

  3. First Position of Target

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

  4. 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 ...

  5. 14. First Position of Target 【easy】

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

  6. [LintCode]——目录

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

  7. CSharpGL(15)用GLSL渲染2种类型的文字

    CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...

  8. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

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

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

随机推荐

  1. toString()方法简单分析

    问题描述 今天在使用spotbugs代码走查时发现这样一个问题,如下, String[] myArray=new String[] {"1","2"," ...

  2. javaweb(三十九)——数据库连接池

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  3. 探究linux设备驱动模型之——platform虚拟总线(二)

    上回说到,platform_match是驱动和设备之间的媒人婆,那么platform_match是如何匹配驱动和设备的呢?platform总线定义的匹配条件很简单,主要就是查看驱动结构体和设备结构体的 ...

  4. Python接口测试实战4(上) - 接口测试框架实战

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

  5. i3wm随笔 1

    快捷键 mod+0 退出 mod+v 垂直分割 mod+h 水平风格

  6. 基础的Servlet

    1.认识Servlet 今天接触了Servlet,我就写了这篇Servlet的文章.首先,我们了解一下Servlet是什么: 这是百度百科的解释,我个人理解是可以用来前后端交互的一个东西,例如可以实现 ...

  7. RetinaNet 迁移学习到自标数据集

    Keras-RetinaNet 在自标数据集 alidq 上训练 detection model RetinaNet 模型部署与环境配置 参考README 数据预处理 数据统计信息: 类别:gun1, ...

  8. Redis 总结精讲 看一篇成高手系统4

    本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...

  9. Qt应用程序重启

    重启应用程序是一种常见的操作,在Qt中实现非常简单,需要用到QProcess类一个静态方法: // program, 要启动的程序名称 // arguments, 启动参数 bool startDet ...

  10. Week4_Linux书本一二两章

    第一章的学习内容就是对Linux内核有一个基本的了解,同时知道一些关于Linux的知识. 学习Linux,可以自己有一台装有Linux操作系统的机器,源代码的作用无可替代: Linux发展历程简介:L ...