Climbing Stairs Solutions in Java
Number 70
Difficulty Easy
Acceptance 47.8%
Link LeetCode
Solutions
Java solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/climbing-stairs/// Inspired by : http://www.jiuzhang.com/solutions/climbing-stairs/// Author : Lei Cao// Date : 2015-10-12package dynamicProgramming.climbStairs;public class climbStairs {/*** @param n: An integer* @return: An integer*/public int climbStairs(int n) {int[] matrix = new int[n];if (n < 3) {return n;}matrix[0] = 1;matrix[1] = 2;// write your code herefor (int i = 2; i < matrix.length; i++) {matrix[i] = matrix[i-1] + matrix[i-2];}return matrix[n-1];}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-18import java.util.Arrays;/// Memory Search/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution1 {private int[] memo;public int climbStairs(int n) {if(n <= 0)throw new IllegalArgumentException("n must be greater than zero");memo = new int[n+1];Arrays.fill(memo, -1);return calcWays(n);}private int calcWays(int n){if(n == 0 || n == 1)return 1;if(memo[n] == -1)memo[n] = calcWays(n - 1) + calcWays(n - 2);return memo[n];}public static void main(String[] args) {System.out.println((new Solution1()).climbStairs(10));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-18/// Dynamic Programming/// Time Complexity: O(n)/// Space Complexity: O(n)public class Solution2 {public int climbStairs(int n) {if(n <= 0)throw new IllegalArgumentException("n must be greater than zero");int[] memo = new int[n + 1];memo[0] = 1;memo[1] = 1;for(int i = 2 ; i <= n ; i ++)memo[i] = memo[i - 1] + memo[i - 2];return memo[n];}public static void main(String[] args) {System.out.println((new Solution2()).climbStairs(10));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20/// Fibonacci Number/// We can see from the transition equation that/// climbStairs(i) is the (i+1)th fibonacci number/// where f0 = 0, f(1) = 1, f(2) = 1, f(3) = 2...////// Time Complexity: O(n)/// Space Complexity: O(1)public class Solution3 {public int climbStairs(int n) {if(n <= 0)throw new IllegalArgumentException("n must be greater than zero");if(n == 1)return 1;int prev = 1, cur = 1;for(int i = 3 ; i <= n + 1; i ++){int f = cur + prev;prev = cur;cur = f;}return cur;}public static void main(String[] args) {System.out.println((new Solution3()).climbStairs(10));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20/// Fibonacci Number - Binets Method/// | F(n+1) F(n) | = | 1 1 |^n/// | F(n) F(n-1) | | 1 0 |////// Time Complexity: O(logn)/// Space Complexity: O(1)public class Solution4 {public int climbStairs(int n) {if(n <= 0)throw new IllegalArgumentException("n must be greater than zero");if(n == 1)return 1;int[][] base = {{1, 1}, {1, 0}};return matrix_pow(base, n)[0][0];}private int[][] matrix_pow(int[][] m, int n){if(n == 1)return m;int[][] t = matrix_pow(m, n / 2);int[][] res = matrix_multiply(t, t);if(n % 2 == 1)return matrix_multiply(res, m);return res;}int[][] matrix_multiply(int[][] m1, int[][] m2){int[][] res = new int[2][2];res[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0];res[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1];res[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0];res[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1];return res;}public static void main(String[] args) {System.out.println((new Solution4()).climbStairs(10));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/climbing-stairs/description//// Author : liuyubobobo/// Time : 2017-11-20/// Fibonacci Number - Closed Formula/// Fn = 1/sqrt(5) * {[(1+sqrt(5))/2]^n - [(1-sqrt(5))/2]^n}////// Time Complexity: O(logn)/// Space Complexity: O(1)public class Solution5 {public int climbStairs(int n) {if(n <= 0)throw new IllegalArgumentException("n must be greater than zero");if(n == 1)return 1;double sqrt5 = Math.sqrt(5.0);return (int)((Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1)) / sqrt5);}public static void main(String[] args) {System.out.println((new Solution5()).climbStairs(10));}}