zoukankan      html  css  js  c++  java
  • 剑指Offer-变态跳台阶

    题目描述

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

    思路

    思路一:

    用递归求解

    $f(n) = f(n-1) + f(n-2) + ... + f(1)$

    $f(n-1) = f(n-2) + ... + f(1)$

    $f(n) = 2*f(n-1)$

    思路二:

    通过方程$f(n)=2^{n-1}$,直接计算跳上一个n级的台阶总共有多少种跳法

    代码实现

    package Recursion;
    
    /**
     * 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
     */
    public class Solution05 {
        public static void main(String[] args) {
            Solution05 solution05 = new Solution05();
            System.out.println(solution05.JumpFloorII_2(3));
    
        }
    
        /**
         * 用递归求解
         * f(n) = f(n-1) + f(n-2) + ... + f(1)
         * f(n-1) = f(n-2) + ... + f(1)
         * f(n) = 2*f(n-1)
         *
         * @param target 台阶数
         * @return 跳法
         */
        public int JumpFloorII(int target) {
            if (target <= 1) {
                return 1;
            } else if (target <= 2) {
                return 2;
            } else {
                return 2 * JumpFloorII(target - 1);
            }
        }
    
        /**
         * f(n)=2^(n-1)
         *
         * @param target 台阶数
         * @return
         */
        public int JumpFloorII_2(int target) {
            //通过移位计算2的次方
            return 1 << (target - 1);
        }
    
    }
    
    
  • 相关阅读:
    恭介的法则
    229. Majority Element II
    169. Majority Element
    233. Number of Digit One
    172. Factorial Trailing Zeroes
    852. Peak Index in a Mountain Array
    162. Find Peak Element
    34. Find First and Last Position of Element in Sorted Array
    81. Search in Rotated Sorted Array II
    33. Search in Rotated Sorted Array
  • 原文地址:https://www.cnblogs.com/wupeixuan/p/8623127.html
Copyright © 2011-2022 走看看