zoukankan      html  css  js  c++  java
  • 《剑指offer》 跳台阶

    本题来自《剑指offer》 跳台阶

    题目1:

      一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

    思路:

      同上一篇。

    C++ Code:

    class Solution {
    public:
        int jumpFloor(int number) {
            int one = 0;
            int two = 1;
            int floor = 0;
            for (unsigned int i =1;i<=number;i++){
                floor = one + two;
                one = two;
                two = floor;
            }
            return floor;
        }
    };

    Python Code:

    # -*- coding:utf-8 -*-
    class Solution:
        def jumpFloor(self, number):
            # write code here
            one = 1
            two = 1
            for i in range(number):
                one,two = two,one+two
            return one

    题目2:变态跳台阶

      一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

    思路:

      归纳总结共有2的n-1次方种跳法,所以直接返回数学表达式。

    Python code:

    # -*- coding:utf-8 -*-
    class Solution:
        def jumpFloorII(self, number):
            # write code here
            floor = 1
            for i in range(number-1):
                floor *= 2
            return floor
  • 相关阅读:
    计算最大公约数 Exercise05_14
    求满足n^2>12000的n的最大值 Exercise05_13
    依赖注入(DI)
    spring容器
    基于xml文件的bean的配置
    小试牛刀 spring的HelloWorld
    spring 装配Bean
    spring介绍
    hibernate相关类与接口
    hibernate 预习
  • 原文地址:https://www.cnblogs.com/missidiot/p/10762362.html
Copyright © 2011-2022 走看看