博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode学习笔记(70. 爬楼梯)
阅读量:4049 次
发布时间:2019-05-25

本文共 845 字,大约阅读时间需要 2 分钟。

在这里插入图片描述

方法一:递归法(超出时间限制)

class Solution {
private: int recursive(int n){
if(n==0||n==1) return 1; return recursive(n-1)+recursive(n-2); }public: int climbStairs(int n) {
return recursive(n); }};

方法二:记忆化搜索

class Solution {
private: vector
memo; int recursive(int n){
if(n==0||n==1) return 1; if(memo[n] == -1) memo[n] = recursive(n-1)+recursive(n-2); return memo[n]; }public: int climbStairs(int n) {
memo = vector
(n+1,-1); return recursive(n); }};

方法三:动态规划

class Solution {
public: int climbStairs(int n) {
vector
memo(n+1,-1); memo[0] = memo[1] = 1; for(int i=2;i<=n;i++) memo[i] = memo[i-1]+memo[i-2]; return memo[n]; }};

转载地址:http://dvyci.baihongyu.com/

你可能感兴趣的文章
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
微信小程序开发全线记录
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>