【LeetCode从零单排】No70.ClimbingStairs

题目

          爬楼梯问题,这是一道很有趣的问题。首先看题目:

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?

这道题一共有三个方法实现(我想到的):

1.递归(对应代码climbStairs)

如果楼梯有n层,我们回退到n-2和n-1步的时候,发现climbStairs(n)=climbStairs(n-1)+climbStairs(n-2)。

为什么?因为当还剩两步的时候,有两种情况,走一个2步,或两个1,但是其中一种情况和climbStairs(n-1)有重复,所以得到如上式子。

这种方法的效率较差,leetcode不接受

2.折中算法(对应代码climbStairs3)

通过上面的讲解,其实只要是任意拆分n为n1,n2和n1+1,n2-1两项就可以。climbStairs(n)=climbStairs(n1)*climbStairs(n2)+climbStairs(n1-1)*climbStairs(n2+1)

这种算法效率最优

3.直接算(对应代码climbStairs1)

k代表1的个数,k从0~n。算出每种情况情况的1步和2步的个数,然后通过牛顿公式算法。但是阶乘较大的时候int型会溢出

代码

package No70.ClimbingStairs;

public class Solution {
    public static int climbStairs(int n) {
        if(n<=0) return 0;
        if(n-3>0){
            return climbStairs(n-2)+climbStairs(n-1);
        }
        else{
             if(n==3) return 3;
              else {
            	 if (n==2){
            		 return 2;
            	 }
            	  else{return 1;}
             }
        }

    }
    public static int climbStairs1(int n) {

    	      if (n==0) return 0;
    	      int k=0;//1的数量
    	      int climbNum=0;
    	      boolean[] isHas=new boolean[n+1];
    	      while(k<=n){
    	    	    int i=(n-k)%2;
    	    	    int j=(n-k)/2;
    	    	    if(isHas[k+i]==false){
    	    	    	  climbNum+=c_cal(j+i+k,i+k);
    	    	    }
    	    	    	isHas[k+i]=true;
    	    	    k++;
    	      }
    	     return climbNum;

    }
    public static int c_cal(int a,int b){
    	   if(b==0) return 1;
    	   int c1=1;
    	   int c2=1;
    	   for(int i=0;i<b;i++){
    		   c1=c1*(a-i);
    		   c2=c2*(b-i);
    	   }

    	   System.out.print(a+":"+b+"   ");

    	   return c1/c2;
    }

    	  public static int climbStairs3(int n) {
    	           if(n <= 3){
    	               return n; 

    	           }
    	           else{
    	               return climbStairs3(n/2)*climbStairs3(n-n/2)+climbStairs3(n/2-1)*climbStairs3(n-n/2-1);
    	               }
    	          }

    public static void main(String[] args){

    	   System.out.print(""+climbStairs3(50));
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-08-31 07:26:51

【LeetCode从零单排】No70.ClimbingStairs的相关文章

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security

【LeetCode从零单排】No26.Remove Duplicates from Sorted Array

题目      题目要求:去除sort int数组中的重复项.      Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory

【LeetCode从零单排】No58.Length of Last Word

题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

【LeetCode从零单排】No.9 Palindrome Number

题目       这道题是迄今为止最快通过的一道题,改了两次就过了,runtime一般(中等偏下,这点不太满意).Palindrome就是判断一个整数是否对称. Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints:Could negative integers be palindromes? (ie, -1) If you are

【LeetCode从零单排】No.135Candy(双向动态规划)

1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more can

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路.记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索.等把这一套盒子都打开完,再打开第二套的.Wikipedia上的讲解是:"Depth

【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路:利用递归找到倒数第一个父节点,记录下它的右节点,将左边的移到右边,然后再把之前标记的右节点连接上. 代码 public class Solution { public

【LeetCode从零单排】No102 Binary Tree Level Order Traversal

题目        Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 return its level order traversal as: [ [3], [9,20], [

【LeetCode从零单排】No 191.Number of 1 Bits(考察位运算)

题目 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). For example, the 32-bit integer '11' has binary representation 00000000000000000000000000001011, so the function should r