1. Description
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?
2. Runtime Distribution
3. Submission Details
5. Code
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active"]
public int climbStairs(int n) { if (n == 1 || n == 2) { return n; } int pre1 = 2, pre2 = 1, sum = 0; for (int k = 3; k <= n; k++) { sum = pre1 + pre2; pre2 = pre1; pre1 = sum; } return sum; }
[/restab]
[/restabs]
6.Test
[restabs alignment="osc-tabs-right" responsive="true" icon="true" text="More" seltabcolor="#fdfdfd" seltabheadcolor="#000" tabheadcolor="blue"]
[restab title="Java" active="active" ]
public class LeetCode0070 { public int climbStairs(int n) { if (n == 1 || n == 2) { return n; } int pre1 = 2, pre2 = 1, sum = 0; for (int k = 3; k <= n; k++) { sum = pre1 + pre2; pre2 = pre1; pre1 = sum; } return sum; } public static void main(String[] args) { LeetCode0070 leetcode = new LeetCode0070(); System.out.println(leetcode.climbStairs(4)); } }
[/restab]
[/restabs]
Comments | NOTHING