美文网首页
leetcode 剑指 Offer 10- I. 斐波那契数列

leetcode 剑指 Offer 10- I. 斐波那契数列

作者: Source_Chang | 来源:发表于2020-11-16 17:02 被阅读0次

    leetcode

    C++:

    class Solution {
    public:
        int fib(int n) {
    
            if ( n < 2 ) {
    
                return n;
            }
    
            int prepre = 0;
            int pre = 1;
            int i = 2;
            while ( i < n ) {
    
                int temp = pre % 1000000007; 
                pre = ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
                prepre = temp;
                
                ++i;
            }
    
            return ( prepre % 1000000007 + pre % 1000000007 ) % 1000000007;
        }
    };
    

    相关文章

      网友评论

          本文标题:leetcode 剑指 Offer 10- I. 斐波那契数列

          本文链接:https://www.haomeiwen.com/subject/tkkwbktx.html