LintCode Binary Search
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的更多相关文章
- CCI4.4/LintCode Balanced Binary Tree, Binary Tree, Binary Search Tree
Binary Tree: 0到2个子节点; Binary Search Tree: 所有左边的子节点 < node自身 < 所有右边的子节点: 1. Full类型: 除最下面一层外, 每一 ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LintCode Search For a Range (Binary Search)
Binary Search模板: mid 和 target 指针比较,left/ right 和 target 比较. 循环终止条件: 最后剩两数比较(while(left + 1 < righ ...
- 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. ...
- 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 ...
- [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 ...
- LintCode题解之Search Range in Binary Search Tree
1.题目描述 2.问题分析 首先将二叉查找树使用中序遍历的方式将元素放入一个vector,然后在vector 中截取符合条件的数字. 3.代码 /** * Definition of TreeNode ...
- 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 ...
- 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 ...
随机推荐
- UNIGUI与UNIURLFRAME的互动
UniSession.JSCode('name_'+MainForm.UniURLFrame1.name+'_'+MainForm.UniURLFrame1.JSName+'.myinput4.inn ...
- iOS中的HTTPS
在WWDC 15,Apple提出的ATS (App Transport Security) ,是 Apple 在推进网络通讯安全的一个重要方式.在 iOS 9 和 OS X 10.11 中,默认情况下 ...
- ReactNative学习笔记(二)基础进阶
一个最简单的HelloWorld页面 先不多解释,直接上代码: import React, { Component } from 'react'; import {AppRegistry, Style ...
- nova scheduler filters 配置
scheduler_driver = nova.scheduler.filter_scheduler.FilterScheduler scheduler_default_filters = Avail ...
- Java 实现将其他类型数据转换成 JSON 字符串工具类
这是网上一个大神实现的,具体出处已找不到,在这做个记录,方便以后使用. package com.wb.test; import java.beans.IntrospectionException; i ...
- Sequelize 学习笔记(11)- Migrations 迁移
一.作用 类似 git 管理源代码 一样,维护你的 DB. 二.安装 npm install --save sequelize-cli 三.使用 1.构建项目时 node_modules/.bin/s ...
- # postgresql-shared_buffers
关于shared_buffers 什么是shred_buffer,我们为什么需要shared_buffers? 1.在数据库系统中,我们主要关注磁盘io,大多数oltp工作负载都是随机io,因此从磁盘 ...
- MySQL Workbench导出Model提示['ERROR 1064 (42000): You have an error in your SQL syntax....syntax to use near 'VISIBLE']
CREATE TABLE IF NOT EXISTS `pihealth`.`warning_events` ( `wid` INT NOT NULL AUTO_INCREMENT, `wtime` ...
- JS闭包与JS函数
先说说在网上看到的一个闭包案例: var add = (function () {var counter = 0;return function () {return counter += 1;}}) ...
- 启动elasticsearch报错
could not find java; set JAVA_HOME or ensure java is in PATH 首先需要安装java 1.yum list installed |grep j ...