Last active
October 13, 2019 15:12
-
-
Save wangchauyan/d94911f143742b2c701f to your computer and use it in GitHub Desktop.
LeetCode - Climb stairs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
You are climbing a stair case. It takes n steps to reach to the top. | |
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | |
*/ | |
public class Solution { | |
public int climbStairs(int n) { | |
if(n == 0 || n == 1) return 1; | |
int finalWays = 0; | |
int f1 = 1; | |
int f2 = 1; | |
for(int i = 2; i <= n; i++) { | |
finalWays = f1 + f2; | |
f1 = f2; | |
f2 = finalWays; | |
} | |
return finalWays. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment