1 line code in c++, 2ms


  • -6
    Z
    class Solution {
    public:
        static double sqrt5;
        static double a;
        static double b;
        static double c;
        int climbStairs(int n) {
            return (int)(a* (pow(b, n+1) - pow(c, n+1)) + 1e-6);
        }
    };
    
    double Solution::sqrt5 = 2.2360679774997896964091736687313;
    double Solution::a = 1/Solution::sqrt5;
    double Solution::b = (1+Solution::sqrt5)/2;
    double Solution::c = (1-Solution::sqrt5)/2;

  • 0
    V

    I do not understand why this solution is down-voted. Using Binet's formula to calculate n-th Fibonacci number is a clever approach IMHO.


Log in to reply
 

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