斐波那契数列
斐波那契数列
http://www.nowcoder.com/questionTerminal/c6c7742f5ba7442aada113136ddea0c3
斐波那契数列
先上代码
public class Solution { public int Fibonacci(int n) { int a = 1; int b = 1; int sum = 0; int k = 2; if(n==1 ||n==2){ return a; } while(k < n){ k++; sum = a + b; a = b; b = sum; } return sum; } }
一开始while语句里写的是k!=n,系统提示超时