Advertisement
kuza2010

Climb stairs solution

Nov 27th, 2021
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.36 KB | None | 0 0
  1. class Solution {
  2.     public int climbStairs(int n) {
  3.         if (n <= 2) {
  4.             return n;
  5.         }
  6.  
  7.         int nCurr = 1;
  8.         int nNext = 2;
  9.         int swap;
  10.  
  11.         for (int i = 3; i < n; i++) {
  12.             swap = nNext;
  13.             nNext = nNext + nCurr;
  14.             nCurr = swap;
  15.         }
  16.  
  17.         return nCurr + nNext;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement