[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就能找到.主要是 ...
随机推荐
- Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...
- SVN Access to ‘/svn/Test/!svn/me’ forbidden,不能更新解决办法
今天上班,使用公司配置的电脑进行项目的更新.SVN报如下错误, SVN Access to '/svn/Test/!svn/me' forbidden,不能更新解决办法 很有意思: 开始以为自己的SV ...
- Android学习笔记——MixLayout
该工程的功能是实现LinearLayout+TableLayout 以下代码是MainActivity.java中的代码 package com.example.mixlayout; import a ...
- docker快速启动脚本
#!/bin/sh PID=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $PID -u -i -n -p n ...
- VBO, VAO, Generic Vertex Attribute
VBO - 用于存储顶点数据的Buffer Object. VAO - 用于组织VBO的对象. Generic Vertex Attribute - 通用顶点属性. For example, the ...
- #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
头文件处理 #import <UIKit/UIKit.h> #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0 #else #imp ...
- XtraFinder在OSX10.11的使用
重启系统,按住command键加上R键 进入恢复模式还是什么模式里,然后启动terminal 然后键入 csrutil enable --without debug 重启电脑,可正常使用 居然上传不了 ...
- Fiddler源代码分享
frmViewer.cs: namespace Fiddler{ using Microsoft.Win32; using System; using System.Collecti ...
- Chrome插件开发
参考文档: http://open.chrome.360.cn/extension_dev/overview.html 参考博文: http://www.cnblogs.com/mfryf/p/370 ...
- 【转】 C++使用zlib库(-)
来自: http://blog.chinaunix.net/uid-24607609-id-2118143.html 今天看到一个gzopen函数,搜了一下他的系列函数,及相关用法 C++使 ...