TOP101题解 | BM62#斐波那契数列#
斐波那契数列
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @author Senky * @date 2023.08.24 * @par url https://www.nowcoder.com/creation/manager/content/584337070?type=column&status=-1 * @brief 时间复杂度为O(n),数据范围[1,40],不适合递归,可以迭代 * @param n int整型 * @return int整型 */ int Fibonacci(int n ) { // write code here if(1 == n || 2 == n) { return 1; } int num1 = 1; int num2 = 1; int temp = 0; for(int i = 2; i < n; i++) { temp = num1 + num2; num1 = num2; num2 = temp; } return temp; }
TIPS:
尽量不用递归,三四十层的时候就会很占用资源,甚至电脑卡顿,多用算法来解决
#TOP101#TOP101-BM系列 文章被收录于专栏
系列的题解