[Leetcode] 70. Climbing Stairs Java

Source: Internet
Author: User

Topic:

You is climbing a stair case. It takes n steps to reach the top.

Each time you can either climb 1 or 2 steps. In what many distinct ways can you climb to the top?

Note:given N would be a positive integer.

test instructions and analysis: There is an n-step ladder, can only climb one or two steps at a time, to find out how many ways to climb the ladder from the total number of methods. The topic of dynamic planning, because there are only two methods to step I (either to climb two steps from i-2 or a step from i-1), so go to step I of D (i) =d (i-1) +d (i-1), that is, to the i-1 step of the walk Plus to the i-2 of the method. So we can find out the solution by traversing to nth step.

Code:

public class Solution {public int climbstairs (int n) {int[] a=new int[n];//used to record the number of steps to take in each step a[0]=1;if (n==1) return 1; a[1]=2;for (int i=2;i<n;i++) {a[i]=a[i-1]+a[i-2];} return a[n-1];}}

  

[Leetcode] 70. Climbing Stairs Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.