[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就能找到.主要是 ...
随机推荐
- 【原】react做tab切换的几种方式
最近搞一个pc端的活动,搞了一个多月,甚烦,因为相比于pc端,更喜欢移动端多一点.因为移动端又能搞我的react了. 今天主要总结一下react当中tab切换的几种方式,因为tab切换基本上都会用到. ...
- 使用Navicat导入导出表的数据做测试(转载)
当我们对MySQL数据库进行了误操作,造成某个数据表中的部分数据丢失时,肯定就要利用备份的数据库,对丢失部分的数据进行导出.导入操作了.Navicat工具正好给我们提供了一个数据表的导入导出功能. 1 ...
- SQL Server Transact-SQL 编程
T-SQL语句用于管理SQL Server数据库引擎实例,创建和管理数据库对象,以及查询.插入.修改和删除数据. Ø 变量 . 局部变量(Local Variable) 局部变量是用户可以自定义的变量 ...
- 删除elasticsearch索引脚本
只保留七天的索引 shell版 #!/bin/bash #hexm@ #只保留一周es日志 logName=( -nginxaccesslog -nginxerrorlog -phperrorlog ...
- easyUI增加视图分组的办法
1.在JSP头文件中引入如下代码 <script type="text/javascript" src="${pageContext.request.context ...
- HTML学习笔记——box
1> HTML写法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- 有关stdint.h 文件
有关stdint.h 文件 Google C++编程规范的P25页有如下叙述: <stdint.h> 定义了 int16_t . uint32_t . int64_t 等整型,在需要确定大 ...
- JS转码
JS转码是解决XSS漏洞的方案,XSS漏洞是指对dom操作时,出现特殊字符造成的安全泄露. XSS漏洞的主要来源有: 1.URL(需要对url进行encodeURIComponent转码). 2.数据 ...
- js控制全屏窗口
<script src="__PUBLIC__/Js/jquery.min.js"></script> <script type="text ...
- Blue tooth
一 . nordic BLE4.0 1.开发nordic的应用需要安装支持keil的pack库和插件 2.nordic的SDK很完整,实例涵盖了几乎所有的应用 https://www.nordicse ...