zoukankan      html  css  js  c++  java
  • 剑指07斐波那契数列

    题目描述

    大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0,第1项是1)。

    n<=39

    class Solution {
    public:
        int Fibonacci(int n) {
            int f=0,g=1;
            while (n--){
                g+=f;
                f=g-f;
            }
            return f;

        }
    };

    public class Solution {
        public int Fibonacci(int n) {
         int target=0;
            if (n==0)
                return 0;
            if (n==1)
                return 1;
            int one=0,two=1;
            for (int i=2;i<=n;i++){
                target=one+two;
                one=two;
                two=target;
            }
            return target;
        }
    }

    # -*- coding:utf-8 -*-
    class Solution:
        def Fibonacci(self, n):
            # write code here
            f1=0
            f2=1
            for i in range(n):
                f1,f2=f2,f1+f2
            return f1
       

  • 相关阅读:
    给你的博客园加个面板娘!
    idea实现简单热部署
    idea 上传svn忽略文件
    谈一谈AOP面向切面编程
    做一个自定义注解
    使用aop切面编写日志模块
    数据结构之链表
    数据结构之队列
    数据结构之栈
    数据结构之线性表
  • 原文地址:https://www.cnblogs.com/hrnn/p/13359009.html
Copyright © 2011-2022 走看看