Interview algorithm question: how many steps are there to climb the stairs ?, Algorithm

Source: Internet
Author: User

Interview algorithm question: how many steps are there to climb the stairs ?, Algorithm
By Long Luo

Personal blog Link

When I recently went to an interview with a small company, the company BOSS gave me an algorithm question:

A person climbs the stairs. One step can take Level 1, level 2, and level 3 steps. If there are N levels of stairs, he needs to write a program and find the total number of steps.

This problem should be a very old question. In middle school mathematics, it isArrangement and combination. After obtaining this question, I first thought of usingRecursionTo solve this problem:

N Level stair problems can be divided into: N-1 level stair, N-2 level stair, N-3 level stair walking method and.

First, calculate the steps 0, 1, 2, 3, and number of stairs:

1 --> 12 --> 11 23 --> 111 12 21 3

The following code can be easily written based on the above analysis:

Public static int countNumber (int stepsNum) {int sum = 0; if (stepsNum = 0) {return 0;} if (stepsNum = 1) {return 1 ;} else if (stepsNum = 2) {return 2;} else if (stepsNum = 3) {return 4;} else if (stepsNum> 3) {return countNumber (stepsNum-3) + countNumber (stepsNum-2) + countNumber (stepsNum-1);} return sum;} public static void main (String [] args) {for (int I = 0; I <= 10; I ++) {System. out. println ("Number of Stair steps:" + I + ", steps:" + countNumber (I ));}}

Let's look at the output:

Number of Stair steps: 0, Walk Method: 0 stair steps: 1, Walk Method: 1 stair steps: 2, Walk Method: 2 stair steps: 3, walk method: 4. Number of Stair steps: 4. Number of Stair steps: 7; number of Stair steps: 5; number of Stair steps: 13; Number of Stair steps: 6; number of Stair steps: 7; number of Stair steps: 7: 44 Number of Stair steps: 8; steps: 81 Number of Stair steps: 9; steps: 149
But how can we solve this problem?

However, it is easy to figure out how many steps are there. Based on this foundation, how to outputSpecific stepsWhat about it?

We can useStackData Structure andRecursionTo complete this question:

Stack <T> is used to save the steps of each step.

The Code is as follows:

/*** A person can climb the stairs in one step, namely Level 1, level 2, and level 3. If there are N levels of stairs, write programs and output all the steps. ** @ Param args */public static void main (String [] args) {Stack <Integer> stt = new Stack <Integer> (); buileT (stt, 3 );} public static void buileT (Stack <Integer> stt, int N) {if (N> = 1) {stt. push (1); buileT (stt, N-1); stt. pop () ;}if (N> = 2) {stt. push (2); buileT (stt, N-2); stt. pop () ;}if (N> = 3) {stt. push (3); buileT (stt, N-3); stt. pop () ;}if (N = 0) {for (int I: stt) {System. out. print ("Step:" + I + "-->");} System. out. println ("finished ");}}
Created by Long Luo at 00:40:12 @ Shenzhen, China. Completed By Long Luo at 18:15:38 @ Shenzhen, China.

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.