zoukankan      html  css  js  c++  java
  • 闲来无事:跳台阶

    题目描述

    一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
     
    第一种,方法递归,估计大家都能想到。
    public class Solution {
        public int JumpFloor(int target) {
            if(target<1) return -1;
            if(target == 1) return 1;
            if(target == 2) return 2;
            return JumpFloor(target-1)+JumpFloor(target-2);
     
        }
    }
    第二种,循环,这个比第一种,可是好很多,原理就是最后一条有两种可能,跳1级,跳2级
    public class Solution {
        public int JumpFloor(int target) {
            if(target <= 0) return 0;
            if(target == 1) return 1;
            if(target == 2) return 2;
            int one = 1;
            int two = 2;
            int result = 0;
            for(int i = 2; i < target; i++){
                result = one+ two;
                one = two;
                two = result;
            }
            return result;
        }
    }

    都是对的,就看要求了。

    梦想还是要有的,万一实现了呢!
  • 相关阅读:
    Ext.form.FieldSet字段集
    jQuery系列目录
    Ext.form.field.Trigger触发字段
    Ext.grid.Panel表格分页
    ExtJS Model数据实体模型
    Ext.form.field.Spinner微调字段
    Ext.window.MessageBox
    书单
    资料收集
    喧嚣
  • 原文地址:https://www.cnblogs.com/jianfeijiang/p/5914269.html
Copyright © 2011-2022 走看看