lintcode Binary Search
来源:互联网 发布:淘宝修真记无弹窗 编辑:程序博客网 时间:2024/05/06 14:43
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
.
Yes
Example
If the array is [1, 2, 3, 3, 4, 5, 10]
, for given target 3
, return 2
.
二分查找
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 int len = array.size(); int i = recurse(array,target,0,len); if (i != -1) { while (array[i-1] == target) { --i; } return i; } return -1; } int recurse(vector<int> &array, int target,int lo,int hi) { if (lo == hi) { return -1; } int mid = (lo + hi)>>1; if (target > array[mid]) { return recurse(array,target,mid+1,hi); } else if (target < array[mid]) { return recurse(array,target,lo,mid); } else { return mid; } }};
0 0
- #LintCode# Binary Search
- lintcode Binary Search
- LintCode Binary Search 二分查找
- lintcode:Validate Binary Search Tree
- lintcode: Unique Binary Search Trees
- [Lintcode]Binary Search Tree Iterator
- LintCode天梯(USGiants)-Binary Search
- Unique Binary Search Trees--lintcode
- lintcode:Search Range in Binary Search Tree
- lintcode: Search Range in Binary Search Tree
- Lintcode - Remove Node in Binary Search Tree
- 20150624 lintcode 总结 Validate Binary Search Tree
- 20150708 lintcode 总结 Binary Search Tree Iterator
- LintCode | Easy | 二分查找 | Binary Search
- lintcode:Remove Node in Binary Search Tree
- lintcode:Unique Binary Search Trees II
- LintCode 86:Binary Search Tree Iterator
- Unique Binary Search Trees II --lintcode
- UI中的协议使用步骤
- 程序员的出路之一
- Android复习笔记(3)-android下的junit
- cloudfs4oss配置
- 白鹭引擎Egret入门知识补充
- lintcode Binary Search
- Java垃圾回收机制
- phpcms V9二次开发之联动菜单筛选 包括box字段的多选 单选 筛选教程
- cocos2d-x 3.0 CPP项目 添加Testin Crash
- Android复习笔记(4) - 文件读取相关
- HDU 2642(树状数组模板二维)
- String StringBuffer StringBuilder比较
- poj 1167 The Buses
- windows10正式版下载安装并激活附最新下载地址