资源经验分享LintCode 题目:斐波纳契数列简单

LintCode 题目:斐波纳契数列简单

2019-11-06 | |  65 |   0

原标题:LintCode 题目:斐波纳契数列简单

原文来自:CSDN      原文链接:https://blog.csdn.net/qq_42410605/article/details/102903430


URL:https://www.lintcode.com/problem/fibonacci-easy/description

描述

Find the Nth number in Fibonacci sequence.

A Fibonacci sequence is defined as follow:

  • The first two numbers are 0 and 1.

  • The i th number is the sum of i-1 th number and i-2 th number.

The first ten numbers in Fibonacci sequence is:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

 

N <= 20

样例

Example 1:
	Input:  1
	Output: 0
	
	Explanation: 
	return the first number in  Fibonacci sequence .
 
Example 2:
	Input:  2
	Output: 1
	
	Explanation: 
	return the second number in  Fibonacci sequence .

 

(1)通过率:100%(使用递归)

在程序中添加方法:

    static int lcc(int x){
            if(x==1)
                return 0;
            else if(x==2)
                return 1;
            else
                return lcc(x-1)+lcc(x-2);
        }

在代码段中添加:

return lcc(n);

即可:
1.png

 

相似题型:LintCode 题目:斐波纳契数列

 

 

免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com

上一篇:目标检测之数据集增强(旋转)

下一篇:C++数据结构二叉树统计总结点个数,叶子结点个数,单分支结点个数,双分支结点个数。(markdown)

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...