斐波拉契数列
题目描述
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
n<=39
思路
递归,或者,使用变量储存前面两个值的斐波那契值。
public class Solution {
public int Fibonacci(int n) {
// if (n==0) return 0;
//if(n==1) return 1;
//return Fibonacci(n-1)+Fibonacci(n-2);
if (n<=1) return n;
int [] mem=new int[n+1];
mem[0]=0;
mem[1]=1;
for(int i=2;i<n+1;i++){
mem[i]=mem[i-1]+mem[i-2];
}
return mem[n];
}
}
矩阵覆盖
题目描述
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
思路
自己去画出来,发现有规律,和波那契数列差不多,当 n 为 1 时,只有一种覆盖方法,当 n 为 2 时,有两种覆盖方法。
public class Solution {
public int RectCover(int target) {
if(target<=2) {
return target;
}
// return RectCover(target-1)+RectCover(target-2);
int pre=1,last=2;
int mem=0;
for(int i=3;i<=target;i++){
mem=pre+last;
pre=last;
last=mem;
}
return mem;
}
}
# -*- coding:utf-8 -*-
class Solution:
def rectCover(self, number):
if number<=2:
return number
pre=1
end=2
count=0
for _ in range(number-2):
count=pre+end
pre=end
end=count
return count
跳台阶
题目描述
一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。
思路同上
public class Solution {
public int JumpFloor(int n) {
if(n<=2) return n;
//return JumpFloor(n-1)+JumpFloor(n-2);
int pre2 = 1, pre1 = 2;
int result = 1;
for (int i = 2; i < n; i++) {
result = pre2 + pre1;
pre2 = pre1;
pre1 = result;
}
return result;
}
}
变态跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
先列举出1个台阶,2个台阶,3个台阶
发现1 个台阶 1种
2个台阶 2种
3 个台阶 4 种,发现这是一个等比数列。
即后面一个事前面一个的两倍。
import java.util.Arrays;
public class Solution {
public int JumpFloorII(int n) {
// if(n<=2 && n>=1) return n;
//return (int)Math.pow(2,n-1);
int [] dp=new int[n];
Arrays.fill(dp,1);
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
dp[i]+=dp[j];
}
}
return dp[n-1];
}
}
# -*- coding:utf-8 -*-
class Solution:
def jumpFloorII(self, number):
# write code here
if number<=2:
return number
list1=[1 for i in range(number)]
for i in range(1,number):
for j in range(i):
list1[i]+=list1[j]
return list1[-1]
旋转数组的最小数字
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
思路是有序的,然后利用2分法查找,然后判读条件是是否比右边的元素大,如果大于右边的元素,证明,小数在右边,然后数组的改变起止位置,然后又2分,比较。。。。。。。。。,直到最小的索引大于大的索引。
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if len(rotateArray)==0:
return 0
i,j=0,len(rotateArray)-1
while i<j:
mid=i+int((j-i)/2)
if rotateArray[mid]<rotateArray[j]:
j=mid
else:
i=mid+1
return rotateArray[i]
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] nums) {
if (nums.length == 0)
return 0;
int l = 0, h = nums.length - 1;
while (l < h) {
int m = l + (h - l) / 2;
if (nums[m] <= nums[h])
h = m;
else
l = m + 1;
}
return nums[l];
}
}
矩阵中的路径
题目描述
请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。
思路
回溯;
import java.util.Arrays;
public class Solution {
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if(rows <=0 || str.length <=0) return true;
boolean[] visited = new boolean[rows*cols];
Arrays.fill(visited,false);
int index = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (hasPathCore(matrix,visited,rows,cols,i,j,str,index)){
return true;
}
}
}
return false;
}
// 核心函数
public boolean hasPathCore(char[] matrix,boolean[] visited,int rows,int cols,int i,int j,char []str,int index){
// 递归退出条件若能到达字符串末尾,则说明访问序列正确
if (index == str.length) return true;
boolean hasPath = false;
// 1.i,j不能越界 2.结点未访问过 3.结点字母与字符串需要的字母一直。
if (i >= 0 && i < rows && j<cols && j >=0 && !visited[i*cols+j] && matrix[i*cols+j] == str[index]){
index++;
visited[i*cols + j] = true;
hasPath = hasPathCore(matrix,visited,rows,cols,i+1,j,str,index) ||
hasPathCore(matrix,visited,rows,cols,i-1,j,str,index) ||
hasPathCore(matrix,visited,rows,cols,i,j+1,str,index) ||
hasPathCore(matrix,visited,rows,cols,i,j-1,str,index);
// 回溯!!! 若不存在路径,则需要将路径点 都重置。
if (!hasPath){
index--;
visited[i*cols+j] = false;
}
}
return hasPath;
}
}
# -*- coding:utf-8 -*-
class Solution:
def hasPath(self,matrix, rows, cols, path):
# write code here
if matrix==None or rows<1 or cols<1 or path==None:
return False
visited=[False for i in range(len(matrix))]
index=0
for i in range(rows):
for j in range(cols):
if self.coreback(matrix,visited,rows,cols,i,j,path,index):
return True
return False
def coreback (self,matrix,visited,rows,cols,i,j,path,index):
if len(path)==index:
return True
hashpath=False
if i>=0 and j>=0 and i<rows and j<cols and not visited[i*cols+j] and matrix[i*cols+j]==path[index]:
visited[i*cols+j]=True
index+=1
hashpath= self.coreback(matrix,visited,rows,cols,i-1,j,path,index) or self.coreback(matrix,visited,rows,cols,i+1,j,path,index) or self.coreback(matrix,visited,rows,cols,i,j-1,path,index) or self.coreback(matrix,visited,rows,cols,i,j+1,path,index)
if not hashpath :
index-=1
visited[i*cols+j]=False
return hashpath