91. 解码方法

本文介绍了一种计算字符串解码方法总数的算法。通过动态规划方法解决了特定编码规则下,如何计算不同解码方式的问题。文章提供了两种实现方案,一种使用O(N)的空间复杂度,另一种则优化至O(1)的空间复杂度。
91. 解码方法

难度:中等

一条包含字母 A-Z 的消息通过以下映射进行了 编码

'A' -> 1
'B' -> 2
...
'Z' -> 26

解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:

  • "AAJF" ,将消息分组为 (1 1 10 6)
  • "KJF" ,将消息分组为 (11 10 6)

注意,消息不能分组为 (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6""06" 在映射中并不等价。

给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数

题目数据保证答案肯定是一个 32 位 的整数。

示例 1:

输入:s = "12"
输出:2
解释:它可以解码为 "AB"(1 2)或者 "L"(12)。

示例 2:

输入:s = "226"
输出:3
解释:它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。

示例 3:

输入:s = "0"
输出:0
解释:没有字符映射到以 0 开头的数字。
含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。
由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。

示例 4:

输入:s = "06"
输出:0
解释:"06" 不能映射到 "F" ,因为字符串含有前导 0("6" 和 "06" 在映射中并不等价)。

提示:

  • 1 <= s.length <= 100
  • s 只包含数字,并且可能包含前导零。

解答:

class Solution {
    //动态规划
    //时间复杂度O(N),空间复杂度O(N)
    public int numDecodings(String s) {
        int n = s.length();
        int[] f = new int[n + 1];
        f[0] = 1;
        for(int i = 1; i <= n; i++){
            if(s.charAt(i - 1) != '0') f[i] += f[i - 1];
            if(i > 1 && s.charAt(i - 2) != '0' && (s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0') <= 26){
                f[i] += f[i - 2];
            }
        }
        return f[n];
    }
}

class Solution {
    //动态规划
    //时间复杂度O(N),空间复杂度O(1)
    public int numDecodings(String s) {
        int n = s.length();
        int a = 0;
        int b = 1;
        int c = 0;
        for(int i = 1; i <= n; i++){
            c = 0;
            if(s.charAt(i - 1) != '0') c += b;
            if(i > 1 && s.charAt(i - 2) != '0' && (s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0') <= 26){
                c += a;
            }
            a = b;
            b = c;
        }
        return c;
    }
}

参考自:

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/decode-ways/solution/jie-ma-fang-fa-by-leetcode-solution-p8np/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

### 关于91解码方法的理解 在讨论91解码算法之前,需先理解其可能涉及的技术背景以及其实现逻辑。根据已知的信息[^1]和量子加密图像处理的相关描述[^2],可以推测91解码可能是某种特定编码方案的逆过程。 #### 解码的核心概念 解码通常是指将经过编码或加密的数据还原为其原始形式的过程。对于91解码而言,假设它是一种基于像素值翻转或其他变换机制的解密技术,则可以通过以下思路来分析: - **数据表示**:如果输入是一个随机量子态(如引用中提到的情况),则该状态可能代表了原图中的某些特征信息,比如像素灰度值或者RGB分量。 - **反转操作**:为了恢复原始图片,在接收到被修改后的比特串之后,需要找到对应位置上发生改变的那个像素点,并将其数值重新设置回初始状态。 以下是基于上述理论构建的一个简单Python程序框架用于演示如何实现类似的“91 Decode Algorithm”。 ```python def nine_one_decode(encoded_message): """ A hypothetical function to demonstrate the concept behind '91 Decoding'. Parameters: encoded_message (list): List containing integers which represent indices where changes occurred. Returns: list: Original unaltered sequence reconstructed from given altered one. """ # Assume this represents our original dataset before any alterations were made original_data = [random.randint(0, 255) for _ in range(len(encoded_message))] decoded_result = [] for i, val in enumerate(encoded_message): if isinstance(val,int)==True and 0<=val<len(original_data): corrected_value=original_data[i]^1 # Example correction logic; actual may vary depending on encoding scheme used decoded_result.append(corrected_value) else: raise ValueError(f"Invalid entry at position {i}: Expected integer between 0-{len(original_data)-1}, got '{repr(val)}'") return decoded_result if __name__ == "__main__": import random test_case=[random.choice([j,k])for j,k in zip(range(10),reversed(range(10)))]*int((sum(abs(x-y)//z+1)for x,y,z in [(a,b,c)for a,b,c in[(m,n,o)p|q|r]]))] # Generate sample data set following some pattern result=nine_one_decode(test_case[:]) print(result) ``` 此脚本仅作为示例展示目的而编写,并不代表真实的91解码流程。真正的应用可能会更加复杂并依赖具体领域内的专业知识和技术细节。 ### 注意事项 由于缺乏关于目标系统的更多具体参数设定方面的资料,以上提供的解决方案具有一定的抽象性和通用性。实际开发过程中还需要考虑诸多因素,例如性能优化、错误检测与纠正能力等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值