青蛙跳台的递归实现




import org.junit.Test;

public class solution {
    @Test
    public void testFunc() throws Exception{
        int res = jumpStar(5);
        System.out.println("res: "+res);
        
    
    }
    private int[] memo=null;
    public int jumpStar(int n){
        memo = new int[n+1];
        memo[1]=1;
        memo[2]=2;
        return jump(n);
    }
    
    public int jump(int n){
        if (n==1) {
            return memo[1];
        }
        if (n==2) {
            return memo[2];
        }
        
        if (memo[n-2]==0) {
            memo[n-2]=jump(n-2);
        }
        if (memo[n-1]==0) {
            memo[n-1]=jump(n-1);
        }
        return memo[n-2]+memo[n-1];
        
    }
    
}

猜你喜欢

转载自blog.csdn.net/wwzheng16/article/details/81018483