18. Up Stairs

Source: Internet
Author: User
Title Description

There is a child on the stairs, the stairs have n steps, the child can be on the 1, 2, 3 order. Please implement a method to calculate how many ways a child has to go upstairs. In order to prevent overflow, please set the result mod 1000000007

Given a positive integer int n, return a number representing the number of ways to go upstairs. Guaranteed n is less than or equal to 100000.

Test Sample: 1
Returns: 1
The code is as follows:
Import java.util.*;p Ublic class Goupstairs {public    int countways (int n) {        int n1=1;        int n2=2;        int n3=4;        int sum=0;       if (n==1) return N1;       if (n==2) return n2;       if (n==3) return n3;       for (int i=4;i<=n;i++) {            sum = ((n3+n2)%1000000007+n1)%1000000007;            N1 = n2;            N2 = n3;            n3 = sum;        }        return sum;    }}

  



18. Up Stairs

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.