动态规划(DP)

动态规划,Dynamic programming,不是Dynamic planning ...

将问题分成若干子问题,前面的解为后面的解所用,注意逆向推倒,找出递推公式,经典的01背包问题,晚点实现一下,这里搞一个LeetCode上面的题放着记录一下

神马求二项式系数,斐波拉契...都可以用动态规划来实现

Climbing Stairs

 

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?

 1 public class Solution {
 2     public int climbStairs(int n) {
 3         int steps[] = new int[n + 1];
 4         for(int i = 0; i <= n; i++){
 5             if(0 == i || 1 == i)
 6                 steps[i] = 1;
 7             else{
 8                 steps[i] = steps[i - 1] + steps[i - 2];
 9             }
10         }
11         return steps[n];
12     }
13 }

01背包问题,待写...

posted on 2014-11-13 15:24  luckygxf  阅读(185)  评论(0编辑  收藏  举报

导航