[LeetCode-interview algorithm classic-Java implementation] [070-Climbing Stairs (Stairs)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [070-Climbing Stairs (Stairs)], leetcode -- java
[070-Climbing Stairs )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original 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?

Theme

You are crawling a stair. You need to take n steps to reach the top. You can take two or one step each time. How many different ways can you climb to the top.

Solutions

Solution 1: Use the combination of numbers to solve the problem. In the following cases, C (0, n) does not take two steps at a time, and C (1, n-1), only two steps, C (2, N-2), until only [n/2] (down to take the whole) Walk two steps. The sum is all solutions.
Solution 2: Use the divide and conquer method to save the solution for n steps using an array. a [1] = 1, a [2] = 2, k> = 2, there is a [k] = a [k-1] + a [K-2].

Code Implementation

Algorithm Implementation class, solution 1

Public class Solution {public int climbStairs (int n) {if (n <0) {return 0;} else {int result = 0; for (int I = 0; I <= n; I ++, n --) {result + = combination (I, n);} return result ;}} /*** calculate the number of combinations ** @ param sup superscript * @ param sub subscript * @ return result */private int combination (int sup, int sub) {if (sup> sub | sup <0 | sub <0) {throw new RuntimeException ("Error args");} if (sup = 0) {return 1;} if (sup> sub/2) {sup = sub-sup;} long x = 1; // the product of the denominator long y = 1; // long z; for (int I = 1; I <= sup; I ++) {x * = (sub-I + 1 ); y * = I; z = gcd (x, y); // find the maximum common denominator. // the denominator of a molecule is reduced to x/= z. y/= z ;} return (int) (x/y);} private int gcd (long min, long max) {long tmp; if (max <min) {tmp = min; min = max; max = tmp;} while (max % min! = 0) {tmp = min; min = max % min; max = tmp;} return (int) min ;}}

Algorithm Implementation class, solution 2

Public class Solution {public int climbStairs (int n) {int result = 0; // only the first-order if (n = 1) {result = 1 ;} // only the Level Two else if (n = 2) {result = 2;} // The level Number of the stairs is greater than 2 else if (n> 2) {// save all solutions int [] ways = new int [n]; ways [0] = 1; ways [1] = 2; for (int I = 2; I <ways. length; I ++) {ways [I] = ways [I-1] + ways [I-2];} result = ways [ways. length-1];} return result ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.
Solution 1

Solution 2

Note Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/47247995]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.