博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Climbing Stairs
阅读量:4071 次
发布时间:2019-05-25

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

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?

在 那个题目中,我们分析出完美情况下,f[i] = f [ i - 1] + f[ i - 2]。这里的情况恰好是完美情况。所以我们可以递归去求解,但n很大的时候递归那么长,有点不合适。

//code

class Solution {public:    int climbStairs(int n) {        if(n <= 1) return n;        int f0 = 1;        int f1 = 1;        int f2;        for(int i = 2; i <= n; ++i)        {            f2 = f1 + f0;            f0 = f1;            f1 = f2;        }        return f2;    }};

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

你可能感兴趣的文章
project web architecture
查看>>
OS + Unix HP-UX
查看>>
OS + Unix Solaris / openSolaris
查看>>
db sql montior
查看>>
Unix + SCO UnixWare
查看>>
db db2 books
查看>>
read humor_campus
查看>>
my read_soft
查看>>
my pdfs
查看>>
framework Schedule Quartz
查看>>
IBM WebSphere Commerce Analyzer
查看>>
Unix + OS IBM Aix System Director
查看>>
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
my read work
查看>>
db db2 base / instance database tablespace container
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>