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. 样例 ...
随机推荐
- shell zsh
之前用fish安装homebrew成功了 但是忘记怎么安装的了 以后要纪录下来了 设置zsh为默认的 shell https://github.com/robbyrussell/oh-my-zsh/w ...
- yii2中gii外网访问的配置方法
if (YII_ENV_DEV) { // configuration adjustments for 'dev' environment $config['bootstrap'][] = 'debu ...
- Beautiful 疑问小记
一.获取id和class的text() html = urlopen(real_url) bsObj = BeautifulSoup(html) h1 = bsObj.h1.get_text() co ...
- ie兼容问题整理
1.连续发请求问题 * jquery(document).ready(function(){}) * 连续发请求ie8出问题,被拦截问题,url后边加时间戳 * 例 url : url+ ...
- 使用easyui时 进入一个新页面 前要经过一个页面混乱的时候 才到正常的页面去
var width = $(window).width(); var height = $(window).height(); var html = "<div id='loading ...
- php支付宝在线支付接口开发教程【转】
php支付宝在线支付接口开发教程 这篇文章主要为大家详细介绍了php支付宝在线支付接口开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.什么是第三方支付 所谓第三方支付,就是一些和各 ...
- Android 如何解决数据库多线程锁的问题
防止多个线程又是读取又是写入 网上找到的方法: 对于这样的问题,解决的办法就是keep single sqlite connection,保持单个SqliteOpenHelper实例,同时对所有数据库 ...
- Android碎片使用
首先新建一个fragment的布局文件, <?xml version="1.0" encoding="utf-8"?><LinearLay ...
- jquery实现自动滚屏效果,适用用公告新闻等滚屏
从网络上找到的例子,自己做了下扩展,原示例是向上滚动,扩展了一个向下滚动的方法: <html xmlns="http://www.w3.org/1999/xhtml"> ...
- DIV设置overflow无效的原因
因为项目需求需要在一个div中添加多个checked 添加的时候使用了 <label><input type="checkbox" value="123 ...