Recursion 爬楼梯问题 @CareerCup
2013-11-26 09:16
459 查看
开始CareerCup一书的刷题。。先从recursion这一章开始!
我的解法:
package Recursion;
/**
* A child is running up a staircase with n steps, and can hop either 1 step, 2
* steps, or 3 steps at a time. Implement a method to count how many possible
* ways the child can run up the stairs.
*
* 一个小孩往上爬一个n层的台阶,他每次可以爬1层,2层或3层。
* 实现一个方法来计算小孩有有多少种方式爬上台阶
*
*/
public class S9_1 {
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
System.out.println(countWaysRec(i));
System.out.println(countWaysDP(i));
}
}
public static int countWaysRec(int n){
if(n == 0){
return 1;
}
if(n == 1){
return 1;
}
if(n == 2){
return 2; // 1个2 或者 2个1
}
if(n == 3){
return 4; // (1个3) 或 (1个1,1个2)或(1个2,1个1)或(3个1)
}
return countWaysRec(n-1) + countWaysRec(n-2) + countWaysRec(n-3);
}
public static int countWaysDP(int n){
int[] ways = new int[n+1];
if(n == 0){
return 1;
}
if(n == 1){
return 1;
}
if(n == 2){
return 2; // 1个2 或者 2个1
}
if(n == 3){
return 4; // (1个3) 或 (1个1,1个2)或(1个2,1个1)或(3个1)
}
ways[0] = 1;
ways[1] = 1;
ways[2] = 2;
ways[3] = 4;
for(int i=4; i<=n; i++){
ways[i] = ways[i-1] + ways[i-2] + ways[i-3];
}
return ways
;
}
}
官方解法:
package Question9_1;
import java.util.HashMap;
/**
* A child is running up a staircase with n steps, and can hop either 1 step, 2 steps,
or 3 steps at a time. Implement a method to count how many possible ways the
child can run up the stairs.
*
*/
public class Question {
public static int countWaysDP(int n, HashMap<Integer, Integer> map) {
if (map.containsKey(n)) {
return map.get(n).intValue();
}
int q = 0;
if (n < 0) {
return 0;
}
if (n == 0) {
q = 1;
} else {
q = countWaysDP(n - 1, map) + countWaysDP(n - 2, map) + countWaysDP(n - 3, map);
}
map.put(n, q);
return q;
}
public static int countWaysRecursive(int n) {
if (n < 0) {
return 0;
} else if (n == 0) {
return 1;
} else {
return countWaysRecursive(n - 1) + countWaysRecursive(n - 2) + countWaysRecursive(n - 3);
}
}
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
long t1 = System.currentTimeMillis();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int c1 = countWaysDP(i, map);
long t2 = System.currentTimeMillis();
long d1 = t2 - t1;
long t3 = System.currentTimeMillis();
int c2 = countWaysRecursive(i);
long t4 = System.currentTimeMillis();
long d2 = t4 - t3;
System.out.println(i + " " + c1 + " " + c2 + " " + d1 + " " + d2);
System.out.println(c2);
}
}
}
我的解法:
package Recursion;
/**
* A child is running up a staircase with n steps, and can hop either 1 step, 2
* steps, or 3 steps at a time. Implement a method to count how many possible
* ways the child can run up the stairs.
*
* 一个小孩往上爬一个n层的台阶,他每次可以爬1层,2层或3层。
* 实现一个方法来计算小孩有有多少种方式爬上台阶
*
*/
public class S9_1 {
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
System.out.println(countWaysRec(i));
System.out.println(countWaysDP(i));
}
}
public static int countWaysRec(int n){
if(n == 0){
return 1;
}
if(n == 1){
return 1;
}
if(n == 2){
return 2; // 1个2 或者 2个1
}
if(n == 3){
return 4; // (1个3) 或 (1个1,1个2)或(1个2,1个1)或(3个1)
}
return countWaysRec(n-1) + countWaysRec(n-2) + countWaysRec(n-3);
}
public static int countWaysDP(int n){
int[] ways = new int[n+1];
if(n == 0){
return 1;
}
if(n == 1){
return 1;
}
if(n == 2){
return 2; // 1个2 或者 2个1
}
if(n == 3){
return 4; // (1个3) 或 (1个1,1个2)或(1个2,1个1)或(3个1)
}
ways[0] = 1;
ways[1] = 1;
ways[2] = 2;
ways[3] = 4;
for(int i=4; i<=n; i++){
ways[i] = ways[i-1] + ways[i-2] + ways[i-3];
}
return ways
;
}
}
官方解法:
package Question9_1;
import java.util.HashMap;
/**
* A child is running up a staircase with n steps, and can hop either 1 step, 2 steps,
or 3 steps at a time. Implement a method to count how many possible ways the
child can run up the stairs.
*
*/
public class Question {
public static int countWaysDP(int n, HashMap<Integer, Integer> map) {
if (map.containsKey(n)) {
return map.get(n).intValue();
}
int q = 0;
if (n < 0) {
return 0;
}
if (n == 0) {
q = 1;
} else {
q = countWaysDP(n - 1, map) + countWaysDP(n - 2, map) + countWaysDP(n - 3, map);
}
map.put(n, q);
return q;
}
public static int countWaysRecursive(int n) {
if (n < 0) {
return 0;
} else if (n == 0) {
return 1;
} else {
return countWaysRecursive(n - 1) + countWaysRecursive(n - 2) + countWaysRecursive(n - 3);
}
}
public static void main(String[] args) {
for (int i = 0; i < 30; i++) {
long t1 = System.currentTimeMillis();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int c1 = countWaysDP(i, map);
long t2 = System.currentTimeMillis();
long d1 = t2 - t1;
long t3 = System.currentTimeMillis();
int c2 = countWaysRecursive(i);
long t4 = System.currentTimeMillis();
long d2 = t4 - t3;
System.out.println(i + " " + c1 + " " + c2 + " " + d1 + " " + d2);
System.out.println(c2);
}
}
}
相关文章推荐
- Recursion 硬币组合问题 @CareerCup
- Recursion 叠箱子最高问题 @CareerCup
- Recursion 二维空间里机器人向右或向下走的所有路径问题 @CareerCup
- Recursion 八皇后问题 @CareerCup
- CareerCup chapter 8 Recursion
- Recursion 字符串的全排列 String Permutation @CareerCup
- Recursion n对括号的组合 @CareerCup
- Recursion 图像软件中的“填充”函数 @CareerCup
- Recursion 计算表达式的括号组合 @CareerCup
- Stack_Queue 汉诺塔Hanoi问题 @CareerCup
- Stack_Queue 猫狗队列问题 Cat dog queue @CareerCup
- CareerCup之1.8 字符串移位包含问题
- Recursion 在数组中寻找神奇下标 @CareerCup
- Recursion 求所有子集 @CareerCup
- Sorting_Searching 读入数字流求rank @CareerCup
- 爬楼梯问题
- CareerCup Find the diameter of the tree
- CareerCup Given a dictionary, how would you add spaces in this string
- CareerCup Randomly return a number inside of this range
- CareerCup Insert 0s to maximize A*B