{ "question_id": 91, "question_title": "解码方法", "difficulty": "中等", "question_content": "

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

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

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

\n\n

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

\n

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

\n

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

\n

 

\n

示例 1:

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

示例 2:

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

示例 3:

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

示例 4:

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

 

\n

提示:

\n", "topic_link": "https://bbs.csdn.net/topics/600469925", "cpp": "#include \n#include \n#include \nstatic int numDecodings(char *s)\n{\n\tint len = strlen(s);\n\tif (len == 0)\n\t{\n\t\treturn 0;\n\t}\n\tint a = 1;\n\tint b = s[0] == '0' ? 0 : a;\n\tint c = b;\n\tfor (int i = 2; i <= len; i++)\n\t{\n\t\tc = s[i - 1] == '0' ? 0 : b;\n\t\tint num = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');\n\t\tif (num >= 10 && num <= 26)\n\t\t{\n\t\t\tc += a;\n\t\t}\n\t\ta = b;\n\t\tb = c;\n\t}\n\treturn c;\n}\nint main(int argc, char **argv)\n{\n\tif (argc != 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test number\\n\");\n\t\texit(-1);\n\t}\n\tprintf(\"%d\\n\", numDecodings(argv[1]));\n\treturn 0;\n}", "java": "class Solution {\n\tpublic int numDecodings(String s) {\n\t\tif (s == null || s.length() == 0) {\n\t\t\treturn 0;\n\t\t}\n\t\tint n = s.length();\n\t\tint[] dp = new int[n + 1];\n\t\tdp[0] = 1;\n\t\tdp[1] = (s.charAt(0) == '0' ? 0 : 1);\n\t\tfor (int i = 1; i < n; i++) {\n\t\t\tchar c = s.charAt(i);\n\t\t\tchar pre = s.charAt(i - 1);\n\t\t\tdp[i + 1] = c == '0' ? 0 : dp[i];\n\t\t\tif (pre == '1' || (pre == '2' && c <= '6')) {\n\t\t\t\tdp[i + 1] += dp[i - 1];\n\t\t\t}\n\t\t}\n\t\treturn dp[n];\n\t}\n}", "js": "", "python": "class Solution(object):\n\tdef numDecodings(self, s):\n\t\t\"\"\"\n\t\t:type s: str\n\t\t:rtype: int\n\t\t\"\"\"\n\t\tls = len(s)\n\t\tif ls == 0:\n\t\t\treturn 0\n\t\tdp = [0] * ls\n\t\tfor index in range(ls):\n\t\t\tif index >= 1 and int(s[index - 1:index + 1]) < 27 and int(s[index - 1:index + 1]) >= 10:\n\t\t\t\tif index == 1:\n\t\t\t\t\tdp[index] = 1\n\t\t\t\telse:\n\t\t\t\t\tdp[index] += dp[index - 2]\n\t\t\tif int(s[index]) != 0:\n\t\t\t\tif index == 0:\n\t\t\t\t\tdp[index] = 1\n\t\t\t\telse:\n\t\t\t\t\tdp[index] += dp[index - 1]\n\t\treturn dp[ls - 1]\n# %%\ns = Solution()\nprint(s.numDecodings(s = \"12\"))", "status": 1, "keywords": "字符串,动态规划", "license": { "cpp": "https://github.com/begeekmyfriend/leetcode", "python": "https://github.com/qiyuangong/leetcode", "java": "https://blog.csdn.net/a1439775520/article/details/104364994" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/90/90_java.ipynb?type=file" }, "notebook_enable": 1 }