[Algorithm Basics] Search
1, Binary Search
On sorted array!
public static int binarySearch(int e, int[] array, int low, int high) {
while(low <= high) {
int mid = (low+high)/2;
if(e == array[mid]) return mid;
else if(e < array[mid]) high = mid -1 ;
else low = mid + 1;
}
return -1;
}
On Rotated sorted array:
while(low <= high) {
int mid = (low+high)/2;
if(array[mid] == e) return mid;
if(array[mid] < array[high]) { //the 2nd half is sorted
if(e > array[mid] && e< array[high])
low = mid+1;
else
high = mid-1;
}
if(array[low] < array[mid]) { //the 1st half is sorted
if(e < array[mid] && e> array[low])
high = mid-1;
else
low = mid+1;
}
}
Rotate array by position x:
Reverse the whole array, then Reverse 0~x-1, then Reverse x~size-1.
2, Fibonacci
f(n)= f(n-1)+f(n-2)
对于实现函数calculateFibo(int index),可以用while loop来逐一计算每个index上的fibo直到到达指定的index;也可以recursive地调用f(n-1) f(n-2)。
对于recursive的方法,类似于一个深度为n的tree,每向下扩展一次结点都会有两个分支,所以O(n)=2*2*2... = 2^n复杂度。
[Algorithm Basics] Search的更多相关文章
- [Algorithm] A* Search Algorithm Basic
A* is a best-first search, meaning that it solves problems by searching amoung all possible paths to ...
- [Algorithm Basics] Sorting, LinkedList
Time complexity: Binary search O(log2 n): i=0. n elements: ------------------- i=1. n/2 ...
- [Math] Beating the binary search algorithm – interpolation search, galloping search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- Algorithm | Binary Search
花了半天把二分查找的几种都写了一遍.验证了一下.二分查找的正确编写的关键就是,确保循环的初始.循环不变式能够保证一致. 可以先从循环里面确定循环不变式,然后再推导初始条件,最后根据循环不变式的内容推导 ...
- 【pat】algorithm常用函数整理
reference is_permutation Test whether range is permutation of another Parameters first1, last1 Input ...
- 本人AI知识体系导航 - AI menu
Relevant Readable Links Name Interesting topic Comment Edwin Chen 非参贝叶斯 徐亦达老板 Dirichlet Process 学习 ...
- [C7] Andrew Ng - Sequence Models
About this Course This course will teach you how to build models for natural language, audio, and ot ...
- BPF for storage:一种受外核启发的反式
BPF for storage:一种受外核启发的反式 译自:BPF for storage: an exokernel-inspired approach BPF主要用于报文处理,通过绕过网络栈提高报 ...
随机推荐
- js中cookie的使用
js中并没有封装好的存储cookie,取得cookie和删除cookie的函数,所以必须得自己手动处理,并且cookie中也只能存储字符串,不能存储数组等复杂的数据类型. // 添加cookie fu ...
- asp.net 微信支付 错误解决方案
asp.net 微信支付 错误解决方案 在网上看到有人解决方案为: 解决方法 出现这种错误网上查出现有的原因是: 订阅号没有相关的权限 账号没有认证,没有相关的权限 那么这里遇到问题两种都不是.开发账 ...
- Python3基础 not in列表名 判断一个元素是否不在列表中列表中
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- 3. Swift 数组|字典|集合
在OC中数组,字典,集合有自己的表示方法,分别是Array,Dictionary,Set 与 String 都属于数值类型变量,他们都属于结构体. 使用简介灵活多变,个人感觉可读性变差了很多,用起来由 ...
- 初识onselectstart
onselectstart几乎可以用于所有对象,其触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中). 实例: 在做拖拽效果的时候,为了防止js选中页面上的其他元素,onselect ...
- 学习mongo系列(九)索引,聚合,复制(副本集),分片
一.索引 二.聚合 三.复制(副本集) 四.分片 尚未实践操作. 详见http://www.runoob.com/mongodb/mongodb-indexing.html
- 监控 Linux Unix Solaris AIX, swap page in / swap page out
vmstat 的 pi/po si/so --监控一天 vmstat 5 17280> vmstat.txt sar -W 1.得到数据 (linux 的 /var/log/sar/saX 自带 ...
- 解决:“java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myMethod”问题!
Spring版本:2.5.6 AspectJ是Spring自带的lib. Jdk版本:1.7.0_17 在配置没问题的情况下,报:java.lang.IllegalArgumentException: ...
- Yii批量添加的问题
使用Yii进行批量添加的时候,执行后会发现表中只插入了foreach循环的最后一条数据,而其它数据没有添加成功,那是因为内存地址中循环时新一条数据会覆盖前一条数据,解决办法如下: 第一种方法: < ...
- c#网络通信框架networkcomms内核解析 序言
NetworkComms网络通信框架序言 networkcomms是我遇到的写的最优美的代码,很喜欢,推荐给大家:) 基于networkcomms2.3.1开源版本( gplv3)协议,写了一些文章, ...