LintCode First Position of Target
找指定target的最左位置。
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 == null) return -1;
if(nums.length == 0) return -1;
int left = 0; int right = nums.length -1;
while(left + 1 < right){
int mid = (left + right) / 2;
if(nums[mid] < target){
left = mid;
}
else if(nums[mid] > target){
right = mid;
}
else if(nums[mid] == target){
right = mid;
}
}
if(nums[left] == target){
return left;
}
else if(nums[right] == target){
return right;
}
else return -1;
}
}
LintCode First Position of Target的更多相关文章
- 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 ...
- [lintcode 14] First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- 14. First Position of Target 【easy】
14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...
- Last Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- lintcode:Binary Search 二分查找
题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...
随机推荐
- RPM安装rabbitMQ
系统使用的是centos 7 - minimal 建立用户和组: # groupadd rabbitmq # useradd rabbitmq -g rabbitmq 在安装rabbitMQ之前需要先 ...
- MultiProvider
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- xshell 终端窗口目录显示为深蓝色的不易分辨问题
xshell更改终端窗口目录展示深蓝色的不易分辨 经常使用xshell远程连接服务器,使用ls命令,目录的颜色都是深蓝色, 如果终端窗口背景颜色是黑色的(对眼睛较好的黑色的背景色,大家一般都选择黑色背 ...
- jquery总结05-常用事件02-表单事件
表单事件 .focus()元素获得焦点时 阻止冒泡 子元素不可以 .blur() 元素失去焦点时 阻止冒泡 子元素不可以 .change() input.select.textarea值发生改变时 i ...
- 学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)
一.备份 在Mongodb中我们使用mongodump命令来备份MongoDB数据.该命令可以导出所有数据到指定目录中. mongodump命令可以通过参数指定导出的数据量级转存的服务器. mongo ...
- Bootstrap结合BootstrapTable的使用,分为两种模试显示列表。 自适应表格
引用的css: <link href="@Url.Content("~/Css/bootstrap.min.css")" rel="styles ...
- js判断IE浏览器版本
if(navigator.userAgent.indexOf("MSIE")>0){ if(navigator.userAgent.indexOf("MSIE 6. ...
- [maven] 使用Nexus创建maven私有仓库
1.为什么需要maven私有仓库? 从Maven中央仓库下载所需的jar包,需要外网的支持.如果公司不能上外网的话则不能从中央仓库下载所需jar包,公司网速慢的时候也会影响项目构建的速度.用户可以用n ...
- jQuery1.9为动态添加元素绑定事件以及获取和操作checkbox的选择属性
1.jQuery为动态添加的元素绑定事件:在1.7之后,添加了live()方法,1.9后又被移除,1.9中可用on()方法: $(function() { $('.btn').on('click', ...
- C++ const、volatile、mutable的用法 (转)
const.volatile.mutable的用法 鸣谢作者: http://blog.csdn.net/wuliming_sc/article/details/3717017 const修饰普通 ...