[lintcode 14] First 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.
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
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.length == 0) {
return -1;
}
int start = 0, end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
end = mid;
}else if (nums[mid] < target) {
start = mid;
}else {
end = mid;
}
}
if (nums[start] == target) {
return start;
}
if (nums[end] == target) {
return end;
}
return -1;
}
}
[lintcode 14] First Position of Target的更多相关文章
- 14. First Position of Target 【easy】
14. First Position of Target [easy] For a given sorted array (ascending order) and a targetnumber, f ...
- LintCode First Position of Target
找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...
- 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 ...
- First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- Last Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number ...
- LintCode Search Insert Position
找出指定target的位置(没有此数时为按顺序应当位置). public class Solution { /** * param A : an integer sorted array * para ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
随机推荐
- SSM的各个配置文件
SqlMapConfig.xml文件:(这是带了mybatis的分页插件的配置) <?xml version="1.0" encoding="UTF-8" ...
- 什么是FOUC?如何避免FOUC?
因为在看一些面试题,所以接触到了这个词 FOUC 什么叫做 FOUC 浏览器样式闪烁 如果使用import方法对css进行导入,会导致某些页面在Windows 下的Internet Explorer出 ...
- uC/OS-II核心(Os_core)块
/*************************************************************************************************** ...
- mysql create db utf8 character
create database if not exists wifi default character set utf8;
- Java——Selector
- string.capwords()函数
string.capwords()函数 string.capwords()函数,有需要的朋友可以参考下. 代码 : import syssys.path.append("C:/Python2 ...
- UnityShader:HSV(色相,饱和度,亮度)转换
http://blog.csdn.net/costfine/article/details/46930473 发现其实美术调整颜色的时候大部分都是调整的HSV,因为可以方便的分别调整色相(hue).饱 ...
- input 获取当前id,name
<input name=" src="toright.png" value="mp3"> <script language=&quo ...
- android 点击屏幕关闭 软键盘
//点击屏幕 关闭输入弹出框 @Override public boolean onTouchEvent(MotionEvent event) { InputMethodManager im = (I ...
- sqlmap写文件为空之谜
恰逢有一个SQL注入可以通过sqlmap进行,而且权限高得离谱,直接就是root权限.既然是root权限当然是想直接getshell咯.可是只是sqlmap -u xxx --os-shell的时候却 ...