Climbing Stairs

Easy

LintCode: http://www.lintcode.com/en/problem/climbing-stairs/

LeetCode: https://leetcode.com/problems/climbing-stairs/#/description

Question

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?

Notice
Given n will be a positive integer.

Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3

Thinking

steps[n] = steps[n-1] + steps[n-2]
If n <= 1, return 1

Solution

Java

public class Solution {
    public int climbStairs(int n) {
        if (n < 1) {
            return 1;
        }
        if (n < 3) {
            return n;
        }
        int step1 = 1;
        int step2 = 2;
        int stepn = 0;
        for (int i = 3; i <= n; i++) {
            int tmp = step2;
            step2 += step1;
            step1 = tmp;
        }
        return step2;
    }
}