lintcode 447 Search in a Big Sorted Array(倍增+二分)
题意:给一个按照升序排序的正整数数组。这个数组很大以至于只能通过固定的接口ArrayReader->get(k)来访问第k个数。并且也没有办法得知这个数组有多大。找到给出的整数target第一次出现的位置。你的算法需要在O(logk)的时间复杂度内完成,k为target第一次出现的位置的下标。如果找不到target,返回-1。
思路:倍增找到第一个大于target的位置,然后二分
class Solve
{
int searchBigSortedArray(ArrayReader* reader,int target)
{
int index = ;
while(reader->get(index-)<target)
index*=;
int start=,end=index;
while(start<=end)
{
int mid = start+(end-start)/;
if(reader->get(mid)==target)
return mid;
else if(reader->get(mid)>target)
end = mid-;
else start = mid+;
}
return -;
}
};
lintcode 447 Search in a Big Sorted Array(倍增+二分)的更多相关文章
- lintcode 447 Search in a Big Sorted Array
Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...
- 【刷题】Search in a Big Sorted Array
原题戳我. 题目 Description Given a big sorted array with positive integers sorted by ascending order. The ...
- Search in Rotated Sorted Array(二分查找)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Find Minimum in Rotated Sorted Array 典型二分查找
https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- [OJ] Find Minimum in Rotated Sorted Array
LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...
- [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】062.Search in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- JDK源代码学习-ArrayList、LinkedList、HashMap
ArrayList.LinkedList.HashMap是Java开发中非常常见的数据类型.它们的区别也非常明显的,在Java中也非常具有代表性.在Java中,常见的数据结构是:数组.链表,其他数据结 ...
- 记录腾讯云中矿机病毒处理过程(重装系统了fu*k)
刚想学学kafka,登录与服务器看看把,谁知ssh特别慢,很奇怪,我以为是我网速问题,断了wifi,换了网线,通过iterm想要ssh root@x.x.x.x,但是上不去? 就tm的很奇怪了,登录腾 ...
- MySQL报错: SQLSTATE[HY000]: General error: 1030 Got error 28 from storage engine
执行命令:df -h [root@iZ25z6qcmrhZ ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/xvda1 40G 38G ...
- error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit status 2
安装mysql是出现这个错误. python3.和python2.两个的版本不一样,所以安装的东西也不一样:MySQLdb 安装mysql的连接包.工具安装 Python3.x版本:Pip insta ...
- for循环里使用查询如何优化(代码库)
for循环里的查询,只是为了赋值对象中的一个字段,如果每一个都重新查一下数据库,影响效率 应该先进行查询,然后再循环里组装自己需要的业务数据 如下代码:list1 查询出对象的一部分内容,list2 ...
- 2.3 os 模块
目录 2.3.1 功能 2.3.2 常用方法 2.3.2.1 创建相关 2.3.2.2 切换相关 2.3.2.3 查看相关 2.3.2.4 编辑相关 2.3.2.5 删除相关 2.3.1 功能 2.3 ...
- Codeforces 1077F1 Pictures with Kittens (easy version)(DP)
题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...
- Codeforces Round #530 (Div. 2) C D
C: *可以保留删除或者增加 ? 保留或者删除 #include<bits/stdc++.h> using namespace std; int main(){ string s; int ...
- 2019南昌邀请赛网络预选赛 M. Subsequence
传送门 题意: 给出一个只包含小写字母的串 s 和n 个串t,判断t[i]是否为串 s 的子序列: 如果是,输出"YES",反之,输出"NO": 坑点: 二分一 ...
- Servlet 学习
3.1 概念 运行在服务器端的小程序 Servlet 就是一个接口 定义JAVA类被浏览器访问(Tomact 识别)的规则 将来 我们需要自定义一个类 实现servlet 接口 重写方法 3.2 快速 ...