1 line code in c++, 2ms

  • -6
    class Solution {
        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

    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.