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. 读懂UML类图

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  2. XAF.web.NewUI:如何自定义主题

    一.使用主题制作工具导出主题: 修改主题生成器工具导出的主题.改完后,导出到 App_Themes 文件夹.例如,更改 ASPxGridView 组面板和Pager面板背景色并保存更改. 使用Them ...

  3. 爬虫2.5-scrapy框架-下载中间件

    目录 scrapy框架-下载中间件 scrapy框架-下载中间件 middlewares.py中有两个类,一个是xxSpiderMiddleware类 一个是xxDownloaderMiddlewar ...

  4. Linux内核学习笔记(7)--完全公平调度(CFS)

    一.完全公平调度算法 完全公平调度 CFS 的出发点基于一个简单的理念:进程调度的效果应该如同系统具备一个理想中的完美多任务处理器.在这种系统中,每个进程能够获得 1/n 的处理器时间(n 为可运行进 ...

  5. win7重装系统后设置Python2.7环境

    起因 台式机的主板莫名出现问题,显示器画面卡顿不能动,鼠标键盘无反应,在这种情况下只好按住电源键断电.下面重启后,显示器无画面,猜测开机后没有进BIOS.然后就拆机箱,拔下电源线后撬起主板电池几秒再放 ...

  6. eos对数据库的操作

    eosio的multi_index 概述 multi_index是eosio上的数据库管理接口,通过eosio::multi_index智能合约能够写入.读取和修改eosio数据库的数据 multi_ ...

  7. app结合unity3D程序中遇到的问题 MapFileParser unity3d导出到IOS程序下 集成unity3dAR功能

    转载自: 来自AR学院(www.arvrschool.com),原文地址为:http://www.arvrschool.com/index.php?c=post&a=modify&ti ...

  8. mysql中latin1编码中文转utf8

    在mysql中,对应的表字段编码通常默认为lartin1编码,在本地客户端显示的时候看着是乱码,但是通过mysql -u -p -h命令登录后,select查询到数据是正常的,通过jdbc或者php等 ...

  9. ES6的新特性(2)——let 与 const 增强变量声明

    let 与 const 增强变量声明 ES6 新增了let命令,用来声明局部变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效,而且有暂时性死区的约束. 先看个var的常见 ...

  10. USACO 2.3.4 Money Systems 货币系统(完全背包)

    Description 母牛们不但创建了他们自己的政府而且选择了建立了自己的货币系统. [In their own rebellious way],,他们对货币的数值感到好奇. 传统地,一个货币系统是 ...