public int fibonacci(int n) {
if (n < 0) return -1; //报错
if (n <= 1) return 1; //第0和第1位都是'1'
int pre1 = 1, pre2 = 1, cur = 2;
for (int i = 2; i <= n; i++) {
cur = pre1 + pre2;
pre2 = pre1;
pre1 = cur;
}
return cur;
}
fibonacci
最新推荐文章于 2019-08-19 23:47:00 发布