Last Position of Target
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的更多相关文章
- [lintcode 14] First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- LintCode First Position of Target
找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...
- First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- 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 ...
- 14. First Position of Target 【easy】
14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- CSharpGL(15)用GLSL渲染2种类型的文字
CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
随机推荐
- Unity CombineTexture
public Texture2D CombineTexture(Texture2D background, Texture2D top) { int width = background.width; ...
- Power Designer逆向工程导入Oracle表,转为模型加注释
1.打开PowerDesigner ——文件——Reverse Engineer——DataBase 2.选择所要连接数据库版本,此处使用的是oracle version 11g. 3.点击红色区域, ...
- 高可用Kubernetes集群-3. etcd高可用集群
五.部署高可用etcd集群 etcd是key-value存储(同zookeeper),在整个kubernetes集群中处于中心数据库地位,以集群的方式部署,可有效避免单点故障. 这里采用静态配置的方式 ...
- 记录一次爬虫报错:Message: Failed to decode response from marionette
由于标题中的错误引发: Message: Tried to run command without establishing a connection 解释: 先说一下我的爬虫架构,用的是firefo ...
- How to Manage Amazon-Fulfilled Orders - Cancel an Amazon-Fulfilled Order
You may request to cancel customer orders that have a status of "Pending" or "Unshipp ...
- 亚马逊AWS业务副总裁:如何在基础设施上降成本
腾讯科技 林靖东 11月17日编译 亚马逊Amazon Web Services业务的副总裁.著名工程师詹姆斯汉密尔顿(James Hamilton)在AWS re:Invent大会上解释了公司是如何 ...
- linux 命令自动补全包
linux 其他知识目录 rhel7如果使用最小化安装后,tab键默认是不能自动补全命令的 执行yum install bash-completion之后重启系统正常.
- 王者荣耀交流协会 - 第7次Scrum会议(第二周)
1.例会照片 照片由王超(本人)拍摄,组内成员刘耀泽,高远博,王磊,王玉玲,王超,任思佳,袁玥全部到齐. 2.时间跨度: 2017年10月26日 17:05 — 17:47 ,总计42分钟. 3.地 ...
- CS小分队第二阶段冲刺站立会议(5月28日)
昨日成果:昨天对我们的软件的主界面进行了思考,考虑到许多人建议我们团队添加可以自主增加软件快捷键的功能,我对这一想法的可行性和项目总体策划进行评估分析后,决定正式实施:已经完成从电脑上添加文件在我们的 ...
- Alpha 冲刺(7/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 学习MSI.CUDA 试运行软件并调试 ...