simple solution


  • 0
    J
    public class Solution {
    	public int climbStairs(int n) {
    		if(n != 1) {
    			int n1 = 1;
    			int n2 = 2;
    			int a = n - 2;
    			//f(n) = f(n-2)+f(n-1);
    			//return f(n);
    			while (a-- > 0) {
    				int tmp = n1;
    				n1 = n2;
    				n2 = n2 + tmp;
    			}
    			return n2;
    		}
    		else {
    			return 1;
    		}
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.