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. 样例 ...
随机推荐
- C library function - freopen()
Description The C library function FILE *freopen(const char *filename, const char *mode, FILE *strea ...
- jquery总结05-常用事件05-触发事件
触发事件 .trigger('click')触发浏览器事件,简写等于.click(),还同时支持自定义事件,并且可以传递参数 $('#elem').on('Aaron', function(event ...
- Nginx模块之————RTMP模块在Ubuntu上以串流直播HLS视频
Nginx的安装在Ubuntu上以串流直播HLS视频 https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video
- 程序设计入门——C语言 第3周编程练习 1 奇偶个数(5分)
1 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,100000).如果 ...
- fsimage 和 edits log
standby NN每隔一段时间(由参数dfs.ha.tail-edits.period决定,默认是60s)去检查Journal node上新的Edits log文件. standby NN每隔一段时 ...
- $inArray()总是返回-1
$inArray(val,arr)注意val类型和arr中的是否一致 如1 和"1" 就会返回-1
- C++ Primer 第九章 顺序容器
由于书籍上写的已经很经典了,故大部分用图片的形式来阐述概念,代码纯手打进行验证. 1.顺序容器类型:vector.deque.list.forword_list.array.string. 2.顺序容 ...
- js 默认选中分页条件项
<table border="0" cellspacing="0" cellpadding="0" height="100% ...
- sqlplus无密码登录TNS协议适配器错误
登录到sqlplus使用无密码登录用户时出现:TNS协议适配器错误 检查自己是否有多个数据库,可能默认登录的数据库服务没有启动,启动即可. 查看当前数据库名 select name from v$d ...
- Django1.9开发博客(9)- 用户认证
你应该注意到了一点,当你去新建.修改和删除文章的时候并不需要登录,这样的话任何浏览网站的用户都能随时修改和删除我的文章.这个可不是我想要的! 编辑和删除的认证 我们需要保护post_new, post ...