Leetcode-LCR 126 斐波那契数

本题答案需要取模 1e9+7(1000000007)

定义一个变量 = 1000000007,答案%变量,完整题目要求

HashMap方法

java 复制代码
class Solution {
    private Map<Integer,Integer> storeMap = new HashMap();
    public int fib(int n) {
        int constant = 1000000007;
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
         if(null != storeMap.get(n)){
            return storeMap.get(n);
        }else{
            int result = fib(n - 1) + fib(n - 2);
            result = result % constant;
            storeMap.put(n,result);
            return result;
        }
    }
}
相关推荐
u0109272713 分钟前
C++中的RAII技术深入
开发语言·c++·算法
2401_832131951 小时前
模板错误消息优化
开发语言·c++·算法
金枪不摆鳍1 小时前
算法--二叉搜索树
数据结构·c++·算法
近津薪荼1 小时前
优选算法——双指针6(单调性)
c++·学习·算法
helloworldandy2 小时前
高性能图像处理库
开发语言·c++·算法
2401_836563182 小时前
C++中的枚举类高级用法
开发语言·c++·算法
bantinghy2 小时前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
chao1898442 小时前
矢量拟合算法在网络参数有理式拟合中的应用
开发语言·算法
代码无bug抓狂人2 小时前
动态规划(附带入门例题)
c语言·算法·动态规划
weixin_445402302 小时前
C++中的命令模式变体
开发语言·c++·算法