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 ...
随机推荐
- SMS模型格网转换为MIKE21的格网源代码
program main !sms网格转换成mike21网格 DIMENSION X(),Y(),H(),NDNN(,),ncbd() dimension NBS(),NOBD(,),NSED(,), ...
- iOS 键盘隐藏
IOS7 点击空白处隐藏键盘的几种方法 IOS开发中经常要用到输入框,默认情况下点击输入框就会弹出键盘,但是必须要实现输入框return的委托方法才能取消键盘的显示,对于用户体验来说很不友好,我 ...
- Activity初步,初学者必看
Activity是什么? Activity是一个可与用户交互并呈现组件的视图.通俗说就是运行的程序当前的这个显示界面. 如果你还不明白,那么你写过HTML吗,它就好比一个网页.我们程序中的用户视图,都 ...
- 日志案例分析(PV,UV),以及动态分区
1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...
- Object C语法学习笔记(一)
1.@property与@synthesize配对使用. @property预编译指令的作用是自动声明属性的setter和getter方法. @synthesize 创建了该属性的访问代码 功能:让编 ...
- [LeetCode] Sudoku Solver(迭代)
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 简单的form表单
效果 html <ul class="edit_list"> <li><em>*</em><span class=" ...
- The Bip Buffer - The Circular Buffer with a Twist
Introduction The Bip-Buffer is like a circular buffer, but slightly different. Instead of keeping on ...
- Subway---poj2502(最短路)
题目链接:http://poj.org/problem?id=2502 人走路的速度是10km/h,地铁的速度是40km/h题目给出一个起点,一个终点,以及几条地铁线路运行的站点.题目给的点的做坐标单 ...
- PresentViewController切换界面(一些系统自带的页面切换动画)
视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...