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. 开发常见错误之 : Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar

    SLF4J: Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackO ...

  2. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

  3. [原]openstack-kilo--issue(一) httpd汇总

    /** 系统环境:redhat7.2 repo:163 openstack version : kilo author: lihaibo **/ 问题1:Invalid command 'group= ...

  4. 部署OpenStack问题汇总(七)--解决apache启动错误"httpd:Could not reliably determine..."

    今天在调试openstack的时候,重启apache,出现以下报错: [root@hctrl log]# service httpd restart 停止 httpd:[确定] 正在启动 httpd: ...

  5. hdu3507 Print Article[斜率优化dp入门题]

    Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)To ...

  6. 【笔记】javascript权威指南-第三章-类型,值和变量

    javascript中的原始类型和对象类型(基本类型和引用类型) //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.27   计算机程序运行时需要对值(value ...

  7. Android开发中Chronometer的用法

    Chronometer集成自TextView,里面有个Handler负责定时更新ui. 其计时原理很简单:通过setBase(long t)方法设置好baseTime之后,当start()时,每隔一秒 ...

  8. &与&&, |与||区别

    &和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...

  9. PPTP不使用远程网关访问公网设置

    使用PPTP拨号的时候默认使用PPTP远程网关访问公网,通过以下设置可以禁止远程网关访问公网 1,右下角选择网络图标右键-属性 2,选择网络IPv4属性,选择属性 3,点击高级选项 4,在远程网络上使 ...

  10. CCCC L2-018. 多项式A除以B 直接上map,然后stack处理输出

    https://www.patest.cn/contests/gplt/L2-018 题意:模拟多项式除法. 题解:短除法,初中奥数老师,高中数学老师,高数老师都讲过2333. 模拟之前,关于保存 多 ...