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 }

转载于:https://www.cnblogs.com/EdwardLiu/p/4339017.html

Climbing Stairs - Print Path相关推荐

  1. 70. Climbing Stairs

    70. Climbing Stairs 1. 题目 You are climbing a stair case. It takes n steps to reach to the top. Each ...

  2. 爬楼梯 · Climbing Stairs

    [抄题]: 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? [思维问题]: 不知道一步.两步怎么加.还是用iteration迭代.此题公式可被 ...

  3. 【动态规划 斐波那切数列】LeetCode 746. Min Cost Climbing Stairs

    LeetCode 746. Min Cost Climbing Stairs 本博客转载自:http://www.cnblogs.com/grandyang/p/8343874.html 存在无代价的 ...

  4. 【斐波那切数列】LeetCode 70. Climbing Stairs

    LeetCode 70. Climbing Stairs 这是一道利用斐波那切数列求解的题目.求斐波那切数列有比较经典的4种方法 (1)递归法:复杂度太高 (2)迭代法:时间复杂度为O(n)O(n)O ...

  5. 算法:Climbing Stairs(爬楼梯) 6种解法

    说明 算法:Climbing Stairs(爬楼梯) LeetCode地址:https://leetcode.com/problems/climbing-stairs/ 题目: You are cli ...

  6. [勇者闯LeetCode] 70. Climbing Stairs

    [勇者闯LeetCode] 70. Climbing Stairs Description You are climbing a stair case. It takes n steps to rea ...

  7. 10.2 动态规划算法套路及空间优化 —— Climbing Stairs Unique Paths

    这一篇文章从最简单的动态规划题目开始,结合上一节动态规划三要素,以LeetCode两道基础的DP题目阐述DP问题的基本套路解法. 70. Climbing Stairs You are climbin ...

  8. [LeetCode]70.Climbing Stairs

    [题目] You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  9. 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. ODP.NET调用存储需要使用事务
  2. python【数据结构与算法】Graph(图)的总结
  3. python对象编程例子-python面向对象编程练习
  4. Docker批量操作容器
  5. 结对项目之需求分析与原型模型设计
  6. GridFS读文件代码示例
  7. 电脑桌面便签_电脑桌面定时提醒记事本便签软件
  8. 硬币 假硬币 天平_小东西叫硬币
  9. Python中类、对象与self详解
  10. 前端学习(1645):前端系列实战课程之留言板功能实现
  11. 基于人人网的Android开发流程介绍
  12. pat1045. Favorite Color Stripe (30)
  13. 【写作技巧】绪论写作要点
  14. 大数据对医学发展有什么影响
  15. 使用 Nginx 服务器配置 flv、mp4,可以直接浏览器播放
  16. 【Vue3.0实战逐步深入系列】vue3.0获取问卷调查结果并输出到控制台
  17. Sklearn.cluster
  18. 微服务构建思路与方法论
  19. 截至2012年5月23日19点58分支持CUDA的NVIDIA的GPU列表(Geforce)
  20. 【论文解读】Mining Dual Emotion for Fake News Detection

热门文章

  1. 动画专业艺术里最懂计算机的,美国数字媒体艺术专业了解一下!
  2. 图神经网络(一)图信号处理与图卷积神经网络(3)图傅里叶变换
  3. oracle tabs作用,Oracle 中 table 函数的应用浅析
  4. 人工智能考题可能性猜测
  5. 玩转mini2440开发板之【编译u-boot提示没有规则可以创建“XX.o”需要的目标】
  6. C/C++中rand()函数产生随机数的用法
  7. 【边缘检测】RCN:Object Contour and Edge Detection with RefineContourNet
  8. 机器学习:贝叶斯和优化方法_Facebook使用贝叶斯优化在机器学习模型中进行更好的实验
  9. 文本摘要提取_了解自动文本摘要-1:提取方法
  10. ubuntu16.04安装CecureCRT 并破解