【LeetCode从零单排】No70.ClimbingStairs
题目
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?
这道题一共有三个方法实现(我想到的):
代码
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));}
}
/********************************
* 本文来自博客 “李博Garvin“
* 转载请标明出处:http://blog.csdn.net/buptgshengod
******************************************/
【LeetCode从零单排】No70.ClimbingStairs相关推荐
- 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)
1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a stree ...
- 【LeetCode从零单排】No26.Remove Duplicates from Sorted Array
题目 题目要求:去除sort int数组中的重复项. Given a sorted array, remove the duplicates in place such that each ...
- 【LeetCode从零单排】No.7 Reverse Integer
前话 今天开始励志刷一下leetcode上面的题目(还好这个网站没被TG和谐).从easy的开始,数一下差不多有40道,争取两个月搞定. 题目 没想到做的第一道题目,虽然看似 ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- 【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 ...
- 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)
背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Se ...
- 【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 ...
- 【LeetCode从零单排】No96 Unique Binary Search Trees
题目 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For ex ...
最新文章
- Windows Server 2012 从入门到精通系列之如何提高DC持续性?
- 安卓开发-Activity中finish() onDestroy() 和System.exit()的区别
- Java连接MySQL数据库 报错
- 2018/7/7-纪中某C组题【jzoj1494,jzoj1495,jzoj1496,jzoj1497】
- line-height与图片底部间隙的学习整理转述
- python面试题总结(1)--语言特性
- fiddler如何设置过滤https_手把手教你如何给网站设置https
- 二十一、K8s集群设置3-HTTPS-Cert-manager
- Linux学习一天一个命令(1)[ls命令]
- [20150228]Delayed Block Cleanout 2.txt
- 独立开发一个完整的小程序,你想知道的流程
- Android 自定义控件之---3D画廊
- ppt流程图箭头分叉_PPT实用模版大全(最全箭头、流程图).ppt
- 汉高将在上海成立新的粘合剂技术创新中心;宁德时代与戴姆勒卡车扩大全球合作伙伴关系 | 美通企业日报...
- 高效工具-requirement生成和配置
- 二进制转化为十进制Java实现
- idea 全局搜索快捷键冲突_intellij idea 的全局搜索快捷键方法
- (二)Tushare Pro教程:上市公司财务数据接口
- 【ALGO】模拟退火(1)
- 打开远程会议模式新篇章,华为云会议让沟通更高效!
热门文章
- 三校生计算机模拟试题1,甘肃省2015年“三校生”考试摸拟试题1
- ktt算法 约化_推荐系统的多目标优化(4)-PE-LTR
- 账号解锁_WOW正式服:周四新CD,解锁账号精华共享的正确姿势
- linux 删除o开头的文件,linux实现除了某个文件或某个文件夹以外的全部删除
- Java中的对象序列化操作
- arouter跨module传递消息_消息队列中间件(二)使用 ActiveMQ
- python计算最大公约数和最小公倍数_python怎么求最大公约数和最小公倍数
- flashcache mysql_flashcache的实现与分析
- 77. Leetcode 1439. 有序矩阵中的第 k 个最小数组和 (堆-技巧二-多路归并)
- ubuntu 运行python subprocess 出现/bin/sh: 1: source: not found 错误