首页 > 其他分享 >每日一题10_动态规划

每日一题10_动态规划

时间:2023-03-07 14:01:43浏览次数:48  
标签:10 return int Solution result 一题 动态 public Math

题目:70. 爬楼梯

思路:

转移方程:斐波那契数列

代码:

class Solution {
    public int climbStairs(int n) {
        //a[n]=a[n-1]+a[n-2],a1=1,a2=2;   关键
        return A(n);   
    }
    int result[]=new int[100000];  //减少重复,降低复杂度
    public int A(int n){
        if(n==1||n==2) {
            return n;
        }
        if(result[n]!=0) return result[n];
        return result[n]=A(n-1)+A(n-2);
        
    }
}

官方解答:

class Solution {
public:
    int climbStairs(int n) {
        int p = 0, q = 0, r = 1;
        for (int i = 1; i <= n; ++i) {
            p = q; 
            q = r; 
            r = p + q;
        }
        return r;
    }
};

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    
public class Solution {
    public int climbStairs(int n) {
        double sqrt5 = Math.sqrt(5);
        double fibn = Math.pow((1 + sqrt5) / 2, n + 1) - Math.pow((1 - sqrt5) / 2, n + 1);
        return (int) Math.round(fibn / sqrt5);
    }
}

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/climbing-stairs/solution/pa-lou-ti-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

image-20230307134944936

标签:10,return,int,Solution,result,一题,动态,public,Math
From: https://www.cnblogs.com/ZLey/p/17187905.html

相关文章

  • Lua 学习-10 元表metaTable
    2.4– MetatablesandMetamethodsEveryvalueinLuacanhavea metatable.This metatable isanordinaryLuatablethatdefinesthebehavioroftheorigina......
  • 106. Construct Binary Tree from Inorder and Postorder Traversal
    题目Giveninorderandpostordertraversalofatree,constructthebinarytree.Note:Youmayassumethatduplicatesdonotexistinthetree.思路本题......
  • MyBatis 动态SQL标签汇总
    if标签<selectid="getEmpByCondition"resultType="com.xy.mybatis.dynamic.pojo.Emp">select*fromempwhere1=1<iftest="name!=''andname!=......
  • Windows10 删除Windows.edb,释放C盘空间
    运行win10系统一段时间后,发现电脑非常卡顿,检查后发现Windows.edb文件占用内存比拟大。Windows.edb一个Window搜索服务数据库文件,索引后提供文件,内容和属性缓存的搜索结果......
  • PAT Basic 1012. 数字分类
    PATBasic1012.数字分类1.题目描述:给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:\(A1\)=能被5整除的数字中所有偶数的和;\(A2\)=将被5除后余......
  • UVA-210 并行程序模拟 题解答案代码 算法竞赛入门经典第二版
    ​​GitHub-jzplp/aoapc-UVA-Answer:算法竞赛入门经典例题和习题答案刘汝佳第二版​​注意:每次unlock后,只需要拿出一个在阻塞队列里面的程序放到等待队列的头部。因为......
  • PAT 乙级 1014 题解 (Basic Level) Practice
    很简单的一道题,我的程序有点乱#include<stdio.h>#include<string.h>#include<ctype.h>intmain(){chars1[61];chars2[61];chars3[61];chars4[61];s......
  • LEETCODE 1096. 花括号展开 II
    这个题把题目中的表达式并列关系看做是求和,把相接看做是求积,那么求解整个表达式的过程可以类比于求解中缀表达式的过程。然后利用两个栈一个存运算符,一个存运算对象classSo......
  • 116、tail+grep命令——2023年3月7日10:01:06
    2023年2月20日14:50:371、tail基本命令tail命令.因为查看日志通常从后面最新的日志去看,tail命令就是从后往前找.比如下述命令会显示access.log的最后10行的内......
  • 车载摄像头ISP实现宽动态HDR
     摄像头ISP的关键信号处理其实前面学习了图像和色彩相关内容,我们可以知道,ISP需要处理的内容还蛮多的,我们最常见的就是畸变校正,白平衡,去噪声、空间转换、WDR合成宽动态......