leetcode70:Climbing Stairs
2016-05-04 09:21
405 查看
2种解法,第一种简单但是会超时,第二种可以AC!!!
package com.leetcode; public class Solution70 { //递归实现的话会超时,并不推荐 public static int climbStairs1(int n){ if( n== 1 ) return 1; if( n == 2 ) return 2; return climbStairs1(n-1 ) + climbStairs1( n -2 ); } //用循环来解决 public static int climbStairs2(int n){ if( n== 1 ) return 1; if( n == 2 ) return 2; //n>=3 int [ ] result = new int[ n+1 ]; result[ 0 ] = 0; result[ 1 ] = 1; result[ 2 ] = 2; for(int i = 3; i <= n; i++){ result[ i ] = result[ i-1 ]+result[ i-2 ]; } return result[ n ]; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(climbStairs1(1)); System.out.println(climbStairs1(2)); System.out.println(climbStairs1(3)); System.out.println(climbStairs1(44)); System.out.println(climbStairs2(1)); System.out.println(climbStairs2(2)); System.out.println(climbStairs2(3)); System.out.println(climbStairs2(44)); } }
相关文章推荐
- 172. Factorial Trailing Zeroes
- lightoj1282 - Leading and Trailing(水题)
- 百度地图 baiduMap(二) 定位
- proxychains-ng 工作原理分析
- mailyWindef.h
- Session 'MainActivity': Error Launching activity
- zzulioj--1842--LT的求助again and again(模拟||LIS)
- OpenAI Gym 关于CartPole的模拟退火解法
- hdu3089 Josephus again(快速约瑟夫环)
- Responder Chain简析
- main()是程序运行的第一个函数吗?
- 小何讲进程: 阻塞进程函数 wait()和waitpid()
- 操作系统raid
- POJ-3216-Repairing Company(最小路径覆盖)
- wait和waitpid函数
- POJ 1422 Air Raid
- Air Infomation System 基于控制台的航班信息系统(方法总结)
- Intel S5000VSA(SAS)主板设置RAID 步骤【转】
- linker command failed with exit code 1 (use -v to see invocation)
- WM_PAINT 消息与 OnDraw、OnPaint()