[LeetCode]70. Climbing Stairs 斐波那契数列&&动态规划

来源:互联网 发布:数据分析师学什么专业 编辑:程序博客网 时间:2024/05/16 11:27

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

斐波那契数列 :f(n)=f(n-1) + f(n-2) ,此题由于并不需要保存中间数据,直接利用参数a,b代替f(n-1) 和f(n-2)

public class LeetCode70 {/** * @param args */ public static int climbStairs(int n) { int res=0; int a=0,b=1; for(int i=0;i<n;i++) { res=a+b; a=b; b=res; } return res; }public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.print(climbStairs(4));}}



0 0
原创粉丝点击