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 解法:

 package fib;

 import java.util.ArrayList;

 public class climbingstairs {

     public ArrayList<String> climb (int n) {
if (n <= 0) return null;
ArrayList<String> res = new ArrayList<String>();
if (n == 1) {
res.add("1");
return res;
}
if (n == 2) {
res.add("2");
res.add("12");
return res;
}
ArrayList<String> former2 = climb(n-2);
for (String item : former2) {
res.add(item+Integer.toString(n));
}
ArrayList<String> former1 = climb(n-1);
for (String item : former1) {
res.add(item+Integer.toString(n));
}
return res;
} public static void main(String[] args) {
climbingstairs obj = new climbingstairs();
ArrayList<String> res = obj.climb(6);
for (String item : res) {
System.out.println(item);
}
} }

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>>

 import java.util.ArrayList;

 public class climbingstairs {

     public ArrayList<String> climb (int n) {
if (n <= 0) return null;
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
for (int i=1; i<=n; i++) {
results.add(new ArrayList<String>());
}
if (n >= 1) {
results.get(0).add("1");
}
if (n >= 2) {
results.get(1).add("2");
results.get(1).add("12");
} for (int i=3; i<=n; i++) {
ArrayList<String> step = results.get(i-1);
ArrayList<String> former2 = results.get(i-3);
for (String item : former2) {
step.add(item+Integer.toString(i));
}
ArrayList<String> former1 = results.get(i-2);
for (String item : former1) {
step.add(item+Integer.toString(i));
}
}
return results.get(n-1);
} public static void main(String[] args) {
climbingstairs obj = new climbingstairs();
ArrayList<String> res = obj.climb(5);
for (String item : res) {
System.out.println(item);
}
} }

Climbing Stairs - Print Path的更多相关文章

  1. LeetCode练题——70. Climbing Stairs

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

  2. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  4. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  5. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  6. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  7. 3月3日(6) Climbing Stairs

    原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...

  8. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

  9. 【LeetCode练习题】Climbing Stairs

    Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...

随机推荐

  1. 4.PHP内核探索:单进程SAPI生命周期

    CLI/CGI模式的PHP属于单进程的SAPI模式.这类的请求在处理一次请求后就关闭.也就是只会经过如下几个环节: 开始 - 请求开始 - 请求关闭 - 结束 SAPI接口实现就完成了其生命周期. 单 ...

  2. laravel 自定义函数 使用

    1.创建app/helpers.php 2.注册路径 { ... "autoload": { "files": [ "app/helpers.php& ...

  3. Why Stored Procedures?

    http://www.w3resource.com/mysql/mysql-procedure.php Stored procedures are fast. MySQL server takes s ...

  4. BLE Device Monitor

    发现 这东西基本是新工具,依赖CC2540 USB Dongle串口来运作 它能做很多事情,扫描设备,研究设备 经验 监控设备躲在这里 官方获得 跑道CC2541页面里去 http://www.ti. ...

  5. 使用OC语言编写两个超大数相乘或相加的算法的思路和超大正整数相乘的代码

    正文: 在编程中,无论是OC还是C亦或是C++语言,所声明的整数变量都会在内存中占有固定的存储空间,而这些存储空间都是固定的. 比如我们知道的int.long.short.unsigend int.u ...

  6. crucible3.x +fisheye3.x 安装和破解

    2015-11-24 22:29 423人阅读 评论(1) 收藏 举报  分类: linux(1)  版权声明:本文为博主原创文章,未经博主允许不得转载. 破解文件下载路径:http://downlo ...

  7. angularJS自定义指令间的“沟通”

    由此例子我们可以看出,angularJS使用指令时link的执行顺序<html> <head> <meta charset="utf-8"/> ...

  8. imx6 kernel clock

    前段时间查看了uboot的时钟,kernel的也稍微了解了下,记录于此,以后再来补充完善. board-mx6q_sabresd.c MACHINE_START(MX6Q_SABRESD, " ...

  9. c# 并行运算

    c# 并行运算 1. Parallel.INVOKE() 看实例: private static Stopwatch watch = new Stopwatch(); private static v ...

  10. Ext4.0 获取选中行及遍历

    var grid= Ext.getCmp('grid'); var records=grid.getSelectionModel().getSelection(); if(records!=null& ...