//二分查找import java.util.ArrayList;
publicclassSolution {publicintminNumberInRotateArray(int [] array) {
if (array.length == 0) {
return0;
}
int low = 0;
int high = array.length - 1;
while (low < high) {
int mid = (low + high) / 2;
if (array[mid] > array[high]) {//注意是拿mid和high比
low = mid + 1;
}elseif (array[mid] < array[high]) {
high = mid;
}else {
high -= 1;
}
}
return array[low];
}
}
7 斐波那契数列
//1,1,2,3,5,8,13,21publicclassSolution {publicintFibonacci(int n) {
if (n == 0) {
return0;
}
int a = 1, b = 1, c = 0;
if (n == 1 || n == 2) {
return1;
}else {
for (int i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
}
return c;
}
}
8 跳台阶
//变版斐波那契数列:1,2,3,5,8,13,21//当前台阶的跳法总数=当前台阶后退一阶的台阶的跳法总数+当前台阶后退二阶的台阶的跳法总数//n级=n-1级的每个方法后面添上1,n-2级的每个方法后面填上2publicclassSolution {publicintJumpFloor(int target) {
if (target <= 0) {
return0;
}
if (target == 1) {
return1;
}
if (target == 2) {
return2;
}
int a = 1;
int b = 2;
int c = 0;
for (int i = 3; i <= target; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
9 变态跳台阶
//n = 之前所以方法的和再加1,//1,2,4,8,16,32publicclassSolution {publicintJumpFloorII(int target) {
if (target <= 0) {
return0;
}
if (target == 1) {
return1;
}
if (target == 2) {
return2;
}
int sum = 3;
int c = 0;
for (int i = 3; i <= target; i++) {
sum = sum + c;
c = sum + 1;
}
return c;
}
}
10 矩形覆盖
//变斐波那契:1,2,3,5,8,13,21;同跳台阶publicclassSolution {publicintRectCover(int target) {
if (target <= 0) {
return0;
}
if (target == 1) {
return1;
}
if (target == 2) {
return2;
}
int a = 1;
int b = 2;
int c = 0;
for (int i = 3; i <= target; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}