
爬楼梯从本科开始就是算法入门的例子思路就像Hello world一样无需多言。class Solution { public int climbStairs(int n) { //特殊情况 if(n1) return 1; //dp及初始化 int[] dp new int[n]; dp[0]1; dp[1]2; for(int i2;in;i){ dp[i]dp[i-1]dp[i-2]; } return dp[n-1]; } }递归写法会超时class Solution { public ListListInteger generate(int numRows) { ListListInteger result new ArrayList(); //动态规划的初始化 ListInteger temp new ArrayList(); temp.add(1); result.add(temp); //i表示result的第几行 for(int i1; inumRows; i){ temp new ArrayList(); //1)第一个 temp.add(1); //2)中间的 for(int j1; ji; j){ temp.add(result.get(i-1).get(j-1)result.get(i-1).get(j)); } //3)最后一个 temp.add(1); result.add(temp); } return result; } }