力扣LCR 165. 解密数字(动态规划)

本文详细介绍了如何通过动态规划解决LCR165问题,涉及构建dp数组来计算不同长度数字序列的翻译方法,以及如何通过isValid函数检查数字位的合法性。文章还分析了时间复杂度和空间复杂度。
摘要由CSDN通过智能技术生成

Problem: LCR 165. 解密数字

题目描述:

在这里插入图片描述

思路

1.每个阶段从1个或者2个数字翻译
2.int dpn + 1 dp[i]表示长度位i的数字序列有多少种翻译方法,到达i这个状态,那上一步只有可能是选择了1个或者两个数字翻译,也就是从状态i - 1, i - 2转换过来,dp[i]的值也有dp[i - 1]和dp[i - 2]推到过来;
3.dp[i] = dp[i - 1] + dp[i - 2];(前提是2个数字不超过25)

解题方法

1.将给定的数字ciphertext存入数组中;
2.编写判断当前两个数字位是否在解密的范围内(函数名为isValid(int a, int b))
3.初始化dp[0] = 1;再从dp数组索引为1的位置开始执行动态转移方程:执行过程中先令dp[i] = dp[i - 1],再判断当前数字位的前两位是否合法(调用编写的isValid函数)
4.返回dp[n]

复杂度

时间复杂度:

O ( n ) O(n) O(n);其中 n n n为给定十进制整形数ciphertext的数字位长度;

空间复杂度:

O ( n ) O(n) O(n)

Code

class Solution {
    /**
     * Get all decryption results(Dynamic programming)
     * int ciphertext: Given number
     */
public:
    int crackNumber(int ciphertext) {
        if (ciphertext <= 9) {
            return 1;
        }
        //Adds the given decimal number to the array
        vector<int> digitList;
        while (ciphertext != 0) {
            digitList.push_back(ciphertext % 10);
            ciphertext /= 10;
        }
        int n = digitList.size();
        int *digits = new int[n];
        for (int i = 0; i < n; ++i) {
            digits[i] = digitList.at(n - i - 1);
        }
        //Represents how many ways to translate a sequence of numbers of length i
        int *dp = new int[n + 1];
        dp[0] = 1;
        for (int i = 1; i <= n; ++i) {
            dp[i] = dp[i - 1];
            if (i - 2 >= 0 && isValid(digits[i - 2], digits[i - 1])) {
                dp[i] += dp[i - 2];
            }
        }
        return dp[n];
    }

    /**
     * Determines whether the current digit can be deduced from the previous two digits
     * int a: The first two digits of the current digit
     * int b: The first digits of current digit
     * return: bool
     */
private:
    bool isValid(int a, int b) {
        if (a == 1) {
            return true;
        }
        if (a == 2 && b >= 0 && b <= 5) {
            return true;
        }
        return false;
    }
};
  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值