zoukankan      html  css  js  c++  java
  • Leecode no.509 斐波那契数

    package leecode;

    /**
    * 509 斐波那契数
    * 斐波那契数,通常用 F(n) 表示,形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
    * F(0) = 0,F(1) = 1
    * F(n) = F(n - 1) + F(n - 2),其中 n > 1
    *
    * 给你 n ,请计算 F(n) 。
    *
    * @author Tang
    * @date 2021/9/13
    */
    public class Fib {

    public int fib(int n) {
    if(n == 0 ){
    return 0;
    }

    int result = 0;

    //构建dp tables
    int n1 = 1;
    int n2 = 1;
    for(int i = 1; i <= n; i++) {
    if(i == 1 || i == 2) {
    result = 1;
    continue;
    }
    result = n1 + n2;
    n2 = n1;
    n1 = result;
    }
    return result;

    }

    public static void main(String[] args) {
    System.out.println(new Fib().fib(3));
    }

    }
  • 相关阅读:
    python3-基础11
    python3-基础10
    python3-基础9
    python3-基础8
    python3-基础7
    python3-基础6
    phaserjs 总结
    ES6总结
    移动端webview调试
    nodejs的理解
  • 原文地址:https://www.cnblogs.com/ttaall/p/15263180.html
Copyright © 2011-2022 走看看