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.

Have you met this question in a real interview? Yes

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 2^32, can your code work properly?

class Solution {
public:
/**
* @param nums: The integer array.
* @param target: Target number to find.
* @return: The first position of target. Position starts from 0.
*/
int binarySearch(vector<int> &array, int target) {
// write your code here
long len = array.size();
long lo = 0;
long hi = len;
while (lo < hi) {
long mid = (lo + hi) / 2;
if (array[mid] < target) {
lo = mid + 1;
} else {
hi = mid;
}
} return (lo == len || array[lo] != target) ? -1 : lo;
}
};

LintCode Binary Search的更多相关文章

  1. CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree

    Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...

  2. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. LintCode Search For a Range (Binary Search)

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

  4. Lintcode: Remove Node in Binary Search Tree

    iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...

  5. Lintcode: Search Range in Binary Search Tree

    Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all t ...

  6. [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 ...

  7. LintCode题解之Search Range in Binary Search Tree

    1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 23.HashMap

    HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做一个整体来处理,系统会根据 ...

  2. 普通java程序,maven打包

    pom.xml文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  3. 关于UIScrollView不能响应UITouch事件的解决办法

    原因是:UIView的touch事件被UIScrollView捕获了. 解决办法:让UIScrollView将事件传递过去.于是最简单的解决办法就是加一个UIScrollView的category.这 ...

  4. [git] 文件操作

    文件操作 1.  git  add 1.1 将未被git管理的文件添加到暂存区 一次可添加多个文件 文件夹,中间用空格隔开 git  add  文件/文件夹  文件/文件夹 再次执行 git  sta ...

  5. C# 获取Header中的token值

    public CurrentUser currentUser { get { CurrentUser result = new CurrentUser(); //jwt 解密token IJsonSe ...

  6. Linux - 在当前系统内查找信息的方法

    查找文本 使用grep命令 grep命令 - 示例 grep命令 - 正则表达式 grep命令 - 统计匹配字符串的行数 grep命令 - 搜索多个单词 结合正则表达式使用grep命令 注意:在搜索指 ...

  7. @pathvariable和@RequestParam的区别

    @PathVariable 获取的是请求路径url中的值: (http://xxx.xxx.com/get_10.html,侧重于请求的URL路径里面的{xx}变量 ) //获取url中某部分的值 @ ...

  8. spring boot -thymeleaf-字符串操作

    以下是一些小测试 <span th:text="${#strings.length(hi)}" ></span> <span th:text=&quo ...

  9. jvm加载类的7个步骤

  10. c/c++本地时间获取

    在记录程序日志时,需要记录时间.如下: #include <iostream> #include <time.h> #include <windows.h> usi ...