牛客题霸--斐波那契数列
斐波那契数列
https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=117&&tqId=34987&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking
题目来源:https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=117&&tqId=34987&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking
来源:牛客
思路:按照斐波那契公式for循环暴力枚举。
代码:
class Solution { public: int Fibonacci(int n) { if(n==0)return 0; if(n==1)return 1; int ans = 0; int n1 = 0; int n2 = 1; for(int i=2;i<=n;i++){ ans=n1+n2; n1=n2; n2=ans; } return ans; } };