From 2d9d6813a1e47f8f9b895dace217ed23dd49645f Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Sat, 17 Mar 2018 16:36:13 +0800 Subject: [PATCH] auto commit --- notes/剑指 offer 题解.md | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index 9c3177b3..9f55503a 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -469,15 +469,15 @@ public class Solution { 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 ```java -public int JumpFloor(int target) { - if (target == 1) return 1; - int[] dp = new int[target]; +public int JumpFloor(int n) { + if (n == 1) return 1; + int[] dp = new int[n]; dp[0] = 1; dp[1] = 2; - for (int i = 2; i < dp.length; i++) { + for (int i = 2; i < n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } - return dp[target - 1]; + return dp[n - 1]; } ``` @@ -488,15 +488,15 @@ public int JumpFloor(int target) { 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级……它也可以跳上 n 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法。 ```java -public int JumpFloorII(int target) { - int[] dp = new int[target]; +public int JumpFloorII(int n) { + int[] dp = new int[n]; Arrays.fill(dp, 1); - for (int i = 1; i < target; i++) { - for (int j = 0; j < i; j++) { + for(int i = 1; i < n; i++) { + for(int j = 0; j < i; j++) { dp[i] += dp[j]; } } - return dp[target - 1]; + return dp[n - 1]; } ``` @@ -507,9 +507,15 @@ public int JumpFloorII(int target) { 我们可以用 2\*1 的小矩形横着或者竖着去覆盖更大的矩形。请问用 n 个 2\*1 的小矩形无重叠地覆盖一个 2\*n 的大矩形,总共有多少种方法? ```java -public int RectCover(int target) { - if (target <= 2) return target; - return RectCover(target - 1) + RectCover(target - 2); +public int RectCover(int n) { + if (n < 2) return n; + int[] dp = new int[n]; + dp[0] = 1; + dp[1] = 2; + for (int i = 2; i < n; i++) { + dp[i] = dp[i - 1] + dp[i - 2]; + } + return dp[n - 1]; } ```