Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/java/climbing_stairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Question: You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Example 1:
Input: n = 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps
*/

//Solution
class Solution {
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
int a = 1;
int b = 1;
int count = 0;
while (n > 1) {
count = a + b;
int temp = b;
b = count;
a = temp;
n--;
}
return count;
}
}

//Submission link: https://leetcode.com/submissions/detail/703917952/