LeetCode之计算解密的数量DecodeWays
来源:互联网 发布:郝斌c语言不压缩百度云 编辑:程序博客网 时间:2024/05/01 13:13
问题描述:
/** * A message containing letters from A-Z is being encoded to numbers using the * following mapping: * * 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, * determine the total number of ways to decode it. * * For example, Given encoded message "12", it could be decoded as "AB" (1 2) or * "L" (12). * * The number of ways decoding "12" is 2. */
在加密中用‘A’来加密为’1’,’B’加密为’2’.但是在解密时,’12’可能被 解密为’AB’(1,2),也可能被解密为’L’(12)。所以给你一串加密后的数字串,问你有多少种解密的方式。
定义数组C[s.length()+1],C[i+1]表示为:到s[0….i]有C[i+1]种解密的方法。
而且有几个限制的条件:
1、s[i-1]不能等于0,若为0,则C[i]=C[i-2];
2、s[i-1,i-2]的数字只能为1~26之间。
例如:
1010,生成的C数组为:[1,1,1,1,1]
10000,生成的C数组为:[1,1,1,0,0,0,0,0,0]
代码如下:
public static int numDecodings(String s){ if(s.length()==0) return 0; int[]c=new int[s.length()+1]; c[0]=1; if(s.charAt(0)!='0') c[1]=c[0]; else c[1]=0; for(int i=2;i<=s.length();i++) { if(s.charAt(i-1)=='0') { c[i]=0; }else{ c[i]=c[i-1]; } if(s.charAt(i-2)=='1'||s.charAt(i-2)=='2'&&s.charAt(i-1)<='6') { c[i]+=c[i-2]; } } return c[s.length()]; }
0 0
- LeetCode之计算解密的数量DecodeWays
- Java-LeetCode-DecodeWays
- LeetCode之岛屿的数量
- 计算三角形的数量
- pygame游戏之旅8:计算躲过的障碍数量
- 计算列的合计数量
- 计算字符串的字符数量
- 如何计算cuboid的数量
- 计算牛的数量-递归
- leetcode 222. Count Complete Tree Nodes 计算满二叉树的节点数量
- leetcode 338. Counting Bits 位计算 + 统计二进制1的数量
- 91-DecodeWays
- LeetCode OJ 之 Candy (糖果数量)
- juery每行单价、数量、金额的计算
- 计算某个字符串包含另一个的数量
- 计算一句话(英文)里面的单词数量
- 计算单词数量的mapreduce程序
- 关于“单元组”数量的计算
- 10以内的简单计算
- Ext.util.Format.date与Ext.Date.format区别, 转换时间戳
- 【电子电路技术】短波与长波红外热像仪区别
- odoo同一个表,怎么从不同菜单进去后显示不同的form
- js事件编程的发布/订阅模式(一对一关系)
- LeetCode之计算解密的数量DecodeWays
- C语言读取文件中文本模式和二进制模式的区别
- transfer error / Failed to push selection: Permission denied 安卓设备文件读写权限不足
- 从服务器获得图片并保存到手机
- 常见的排列组合问题
- 26. Remove Duplicates from Sorted Array
- 深入理解JVM
- 欢迎使用CSDN-markdown编辑器
- 性能分析系列——小命令保证大性能