题目

爬楼梯问题,这是一道很有趣的问题。首先看题目:

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

这道题一共有三个方法实现(我想到的):

1.递归(对应代码climbStairs)
如果楼梯有n层,我们回退到n-2和n-1步的时候,发现climbStairs(n)=climbStairs(n-1)+climbStairs(n-2)。
为什么?因为当还剩两步的时候,有两种情况,走一个2步,或两个1,但是其中一种情况和climbStairs(n-1)有重复,所以得到如上式子。
这种方法的效率较差,leetcode不接受
2.折中算法(对应代码climbStairs3)
通过上面的讲解,其实只要是任意拆分n为n1,n2和n1+1,n2-1两项就可以。climbStairs(n)=climbStairs(n1)*climbStairs(n2)+climbStairs(n1-1)*climbStairs(n2+1)
这种算法效率最优
3.直接算(对应代码climbStairs1)
k代表1的个数,k从0~n。算出每种情况情况的1步和2步的个数,然后通过牛顿公式算法。但是阶乘较大的时候int型会溢出

代码

package No70.ClimbingStairs;public class Solution {public static int climbStairs(int n) {if(n<=0) return 0;if(n-3>0){return climbStairs(n-2)+climbStairs(n-1);}else{if(n==3) return 3;else {if (n==2){return 2;}else{return 1;}}}}public static int climbStairs1(int n) {if (n==0) return 0;int k=0;//1的数量int climbNum=0;boolean[] isHas=new boolean[n+1];while(k<=n){int i=(n-k)%2;int j=(n-k)/2;if(isHas[k+i]==false){climbNum+=c_cal(j+i+k,i+k);}isHas[k+i]=true;k++;}return climbNum;}public static int c_cal(int a,int b){if(b==0) return 1;int c1=1;int c2=1;for(int i=0;i<b;i++){c1=c1*(a-i);c2=c2*(b-i);}System.out.print(a+":"+b+"   ");return c1/c2; }public static int climbStairs3(int n) { if(n <= 3){ return n; } else{return climbStairs3(n/2)*climbStairs3(n-n/2)+climbStairs3(n/2-1)*climbStairs3(n-n/2-1);}}public static void main(String[] args){System.out.print(""+climbStairs3(50));}
}
代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No70.ClimbingStairs相关推荐

  1. 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

    1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...

  2. 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

    题目      题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...

  3. 【LeetCode从零单排】No.7 Reverse Integer

    前话       今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目        没想到做的第一道题目,虽然看似 ...

  4. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  5. 【LeetCode从零单排】No22.Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  6. 【LeetCode从零单排】No221.Maximal Square

    题目 Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  7. 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

    背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...

  8. 【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

    题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

  9. 【LeetCode从零单排】No96 Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex ...

最新文章

  1. Windows Server 2012 从入门到精通系列之如何提高DC持续性?
  2. 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
  3. Java连接MySQL数据库 报错
  4. 2018/7/7-纪中某C组题【jzoj1494,jzoj1495,jzoj1496,jzoj1497】
  5. line-height与图片底部间隙的学习整理转述
  6. python面试题总结(1)--语言特性
  7. fiddler如何设置过滤https_手把手教你如何给网站设置https
  8. 二十一、K8s集群设置3-HTTPS-Cert-manager
  9. Linux学习一天一个命令(1)[ls命令]
  10. [20150228]Delayed Block Cleanout 2.txt
  11. 独立开发一个完整的小程序,你想知道的流程
  12. Android 自定义控件之---3D画廊
  13. ppt流程图箭头分叉_PPT实用模版大全(最全箭头、流程图).ppt
  14. 汉高将在上海成立新的粘合剂技术创新中心;宁德时代与戴姆勒卡车扩大全球合作伙伴关系 | 美通企业日报...
  15. 高效工具-requirement生成和配置
  16. 二进制转化为十进制Java实现
  17. idea 全局搜索快捷键冲突_intellij idea 的全局搜索快捷键方法
  18. (二)Tushare Pro教程:上市公司财务数据接口
  19. 【ALGO】模拟退火(1)
  20. 打开远程会议模式新篇章,华为云会议让沟通更高效!

热门文章

  1. 三校生计算机模拟试题1,甘肃省2015年“三校生”考试摸拟试题1
  2. ktt算法 约化_推荐系统的多目标优化(4)-PE-LTR
  3. 账号解锁_WOW正式服:周四新CD,解锁账号精华共享的正确姿势
  4. linux 删除o开头的文件,linux实现除了某个文件或某个文件夹以外的全部删除
  5. Java中的对象序列化操作
  6. arouter跨module传递消息_消息队列中间件(二)使用 ActiveMQ
  7. python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
  8. flashcache mysql_flashcache的实现与分析
  9. 77. Leetcode 1439. 有序矩阵中的第 k 个最小数组和 (堆-技巧二-多路归并)
  10. ubuntu 运行python subprocess 出现/bin/sh: 1: source: not found 错误