Skip to content

Instantly share code, notes, and snippets.

@wangchauyan
Last active October 13, 2019 15:12
Show Gist options
  • Save wangchauyan/d94911f143742b2c701f to your computer and use it in GitHub Desktop.
Save wangchauyan/d94911f143742b2c701f to your computer and use it in GitHub Desktop.
LeetCode - Climb stairs
/*
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