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 解法:
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的更多相关文章
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 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 ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
随机推荐
- linux 相关系列安装
以Red Hat Enterprise Linux 5为例进行讲解. 相关系列: linux下jdk的安装 linux下ant的安装 linux下redis的安装 linux下svn的安装 linux ...
- session配置理解
session.cache_limiter 指定会话页面所使用的缓冲控制方法,默认为nocache.session.cache_expire 以分钟数指定缓冲的会话页面的存活期,默认为180.此设定对 ...
- add active class
根据URI添加菜单的active css class Active item item in menu: <?php function aim($page) { if(stristr($_SER ...
- ext在web工程目录导致myeclipse内存溢出问题
分类: Extjs2013-01-24 00:01 2068人阅读 评论(2) 收藏 举报 当在eclipse中的web工程中增加了extjs4,出现An internal error occurre ...
- java new synchronized
java provides the synchronized keyword for synchronizing thread access to critical sections. Because ...
- terminal(终端),shell,tty,console(控制台)区别
原文地址 stackexchange:What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'con ...
- 数据库留言板例题:session和cookie区别
session和cookie区别: <?php session_start(); //session_start();必须写在所有的php代码前边 ?> <!DOCTYPE html ...
- Oracle一些基本操作
查看表以及列: Select * From all_tables where owner = 'userName' ---注意,这里需要区分大小写! select * from user_tab_co ...
- 设计模式:观察者模式(Observer)
定 义:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象.这个主题对象在状态发生 变化时,会通知所有观察者对象,使他们能够自动更新自己. 结构图: 抽象主题类: abstract c ...
- gradlew常用命令
./gradlew -v 查看版本 ./gradlew clean 清理.下载依赖 ./gradlew build 构建 libgdx项目中的gradlew run: ./gradlew deskt ...