s***c 发帖数: 1926 | 2 给你看看科班码农怎么思考问题
dp + 滚动数组
public int fib(int n)
{
int a = 0, b = 1, c;
if( n == 0)
return a;
for (int i = 2; i <= n; i++)
{
c = a + b;
a = b;
b = c;
}
return b;
}
非要把c去掉的话,简单的数学计算
public int fib2(int n)
{
int a = 0, b = 1;
if( n == 0)
return a;
for (int i = 2; i <= n; i++)
{
b = a + b;
a = b - a;
}
return b;
}
你再要把i省掉就写成不带i的循环啊
所谓省变量,都是编译器可以干的事情。... 阅读全帖 |
|