Climbing Stairs - Print Path

stair climbing, print out all of possible solutions of the methods to climb a stars, you are allowed climb one or two steps for each time; what is time/space complexity? (use recursion)

这道题难是难在这个ArrayList<String> res是用在argument还是返回值,纠结了好久

Recursion 解法:

 1 package fib;
 2
 3 import java.util.ArrayList;
 4
 5 public class climbingstairs {
 6
 7     public ArrayList<String> climb (int n) {
 8         if (n <= 0) return null;
 9         ArrayList<String> res = new ArrayList<String>();
10         if (n == 1) {
11             res.add("1");
12             return res;
13         }
14         if (n == 2) {
15             res.add("2");
16             res.add("12");
17             return res;
18         }
19         ArrayList<String> former2 =  climb(n-2);
20         for (String item : former2) {
21             res.add(item+Integer.toString(n));
22         }
23         ArrayList<String> former1 = climb(n-1);
24         for (String item : former1) {
25             res.add(item+Integer.toString(n));
26         }
27         return res;
28     }
29
30
31     public static void main(String[] args) {
32         climbingstairs obj = new climbingstairs();
33         ArrayList<String> res = obj.climb(6);
34         for (String item : res) {
35             System.out.println(item);
36         }
37     }
38
39 }

Sample input : 6

Sample Output:

246
1246
1346
2346
12346
1356
2356
12356
2456
12456
13456
23456
123456

follow up: could you change the algorithm to save space?

这就想到DP,用ArrayList<ArrayList<String>>

 1 import java.util.ArrayList;
 2
 3 public class climbingstairs {
 4
 5     public ArrayList<String> climb (int n) {
 6         if (n <= 0) return null;
 7         ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
 8         for (int i=1; i<=n; i++) {
 9             results.add(new ArrayList<String>());
10         }
11         if (n >= 1) {
12             results.get(0).add("1");
13         }
14         if (n >= 2) {
15             results.get(1).add("2");
16             results.get(1).add("12");
17         }
18
19         for (int i=3; i<=n; i++) {
20             ArrayList<String> step = results.get(i-1);
21             ArrayList<String> former2 = results.get(i-3);
22             for (String item : former2) {
23                 step.add(item+Integer.toString(i));
24             }
25             ArrayList<String> former1 = results.get(i-2);
26             for (String item : former1) {
27                 step.add(item+Integer.toString(i));
28             }
29         }
30         return results.get(n-1);
31     }
32
33
34     public static void main(String[] args) {
35         climbingstairs obj = new climbingstairs();
36         ArrayList<String> res = obj.climb(5);
37         for (String item : res) {
38             System.out.println(item);
39         }
40     }
41
42 }
时间: 2024-10-10 02:36:56

Climbing Stairs - Print Path的相关文章

【LeetCode】Climbing Stairs

Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VARCHAR(20) default NULL, last_name VARCHAR(20) default NULL, salary INT default NULL, PRIMARY KEY (id) ); create table CERTIFICATE ( id INT NOT NULL aut

Leetcode-70 Climbing Stairs

#70.    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? 题解:这道题属于动态规划的题,类似于斐波那契数列,所以采用非递归的方式来解.当楼梯只有一级时,显然只有一种方法,即f(1

[LeetCode] Climbing Stairs (Sequence DP)

Climbing Stairs https://oj.leetcode.com/problems/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? 这题比较简单,可以使用动态规划来求解

Climbing Stairs

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 class Solution { 2 public: 3 int climbStairs(int n) { 4 vector<int

[LeetCode][JavaScript]Climbing Stairs

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? https://leetcode.com/problems/climbing-stairs/ 直接递归超时了,要动态规划. 打印前几个数

[LeetCode OJ]-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? 一共有n阶楼梯,每次只能爬1阶或2阶,问爬到顶端一共有多少种方法 方法一: 利用二叉树的思想,对于n=3时有3种方法,有几个叶子节点便有几种方法 1 void clim

[leetcode]Climbing Stairs @ Python

原题地址:https://oj.leetcode.com/problems/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? 解题思路: 爬楼梯问题.经典的动态规划问题.每次上

leetcode 刷题之路 92 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? 一个台阶总共有n级,如果一次可以跳1级,也可以跳2级,求总共有多少总跳法. 分析: 一次最多只能跳两级,那么对于第n级(n>=2)台阶,显然只能从第n-1级跳一级到达或

leetCode 70. Climbing Stairs | 动态规划

70. 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? 思考: top steps 1 1 2 2 3 3 4 5 5 8 6 13 ... ... 从上面的分析可以看出,f(top)