General way for solving Fibonacci sequence. More readable.


  • 4
    S
    class Solution {
    public:
        int climbStairs(int n) {
            long pre = 0; // f(-1)
            long curr = 1; // f(0)
        
            while (n-- > 0)
            {
                curr = pre + curr;
                pre = curr - pre;
            }
        
            return curr;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.