Binary search is a famous question in algorithm.

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.

Example
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2. Challenge
If the count of numbers is bigger than MAXINT, can your code work properly?

跟Leetcode里search for a range挺像的,就是找到一个target之后,还要继续找它的左边沿。最后l指针超过r指针之后, l 指针会停在左边沿上

 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) {
int l = 0, r = nums.length - 1;
int m = 0;
while (l <= r) {
m = (l + r) / 2;
if (nums[m] == target) break;
else if (nums[m] > target) r = m - 1;
else l = m + 1;
}
if (nums[m] != target) return -1;
l = 0;
r = m;
while (l <= r) {
m = (l + r) / 2;
if (nums[m] == target) {
r = m - 1;
}
else l = m + 1;
}
return l;
}
}

Lintcode: First Position of Target (Binary Search)的更多相关文章

  1. LintCode First Position of Target

    找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...

  2. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  3. lintcode 中等题:unique Binary Search Tree 不同的二叉查找树

    题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ \ 3 2 1 ...

  4. LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  5. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  6. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  7. **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  8. LintCode Search For a Range (Binary Search)

    Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...

  9. Binary search for the first element greater than target

    We all know how to search through an array for an element whose value equals the target value, but h ...

随机推荐

  1. gerrit_bash_commands.sh

    https://github.com/tomwys/gerrit-bash-commands gerrit_bash_commands.sh # Author: Tomasz Wysocki < ...

  2. rsyslog local0-local7的用法

    很多时候我们需要将一个服务的日志文件导向一个指定的文件,这个时候可以设置log-facility 如在dhcpd.conf中配置 1 : update log-facility in the dhcp ...

  3. linux route命令详解

    考试题一:linux下如何添加路由(百度面试题) 以上是原题,老男孩老师翻译成如下3道题. a.如何用命令行方式给linux机器添加一个默认网关,假设网关地址为10.0.0.254? b. 192.1 ...

  4. CSS属性绘制图形(一)

    div部分: <div class="react-logo"> <div class="reactive"></div> & ...

  5. 基本类型算法题目学习(EPI)

    1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...

  6. stopImmediatePropagation和stopPropagation (事件、防止侦听)

    参考: ActionScript 3.0 Step By Step系列(六):学对象事件模型,从点击按扭开始 actionscript宝典 一.事件模型 egret中的事件模型和flash是一样的,但 ...

  7. [工具] f.lux – 随时间改变屏幕色温护眼

    f.lux 是一款根据时间变化来改变屏幕色温的软件.让你在深夜也能感受到太阳的温暖,顺便还有助于睡眠. 在 f.lux 里,首先设置一个适合你的变化色温范围,白天的色温控制在 6500K 以下,晚上的 ...

  8. SAP全球企业官孙小群的生活智慧

    转自:http://www.programmer.com.cn/15373/ 一下为程序员杂志对孙小群(Xiaoqun Clever)的采访. 最早接触计算机是在高中,那时发现通过一个小小的Basic ...

  9. npm使用报错解决办法

    在使用脚手架工具进行项目搭建的时候,很多时候会用到npm ,最近用npm的时候遇到一个错误: 'CALL "I:\Program Files\nodejs\\node.exe" & ...

  10. python操作数据库PostgreSQL

    1.简述 python可以操作多种数据库,诸如SQLite.MySql.PostgreSQL等,这里不对所有的数据库操作方法进行赘述,只针对目前项目中用到的PostgreSQL做一下简单介绍,主要包括 ...