Java using formula, one line


  • 0
    H

    The result is the (n + 1)th Fibonacci Number, using the formula in wikipedia

    public class Solution {
        private static final double Phi = (1.0 + Math.sqrt(5)) / 2.0;
        private static final double phi = (1.0 - Math.sqrt(5)) / 2.0;
        private static final double sqrt5 = Math.sqrt(5);
        
        
        public int climbStairs(int n) {
            n = n + 1;
            return (int) Math.round((Math.pow(Phi, n) - Math.pow(phi, n)) / sqrt5);
        }
    }
    

Log in to reply
 

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