LINTCODE——解码方法
来源:互联网 发布:药智网数据库vip 编辑:程序博客网 时间:2024/06/05 14:42
LINTCODE——解码方法
思路:这题有点大意,刚开始漏了好多边界情况,如“1289012”、“123120024”等,我把这两个分别叫做0前数超范围,和双0情况;考虑到边界,然后用动态规划分析可知,在正常情况下,对于给定字符串S,n为S的长度,T(i)为字符串前i个字符的解码数,则不外乎三种情况,T(i)=T(i-1),T(i) = T(i-1)+T(i-2),T(i)=T(i-2),根据情况分类迭代就可以了;
class Solution {public: /* * @param s: a string, encoded message * @return: an integer, the number of ways decoding */ int numDecodings(string &s) { // write your code here int n = s.size(); vector<int> count(n+1,0); //特殊情况,字符串为空或首位为0,边界处理 if(n == 0) return 0; if(s[0] == '0') return 0; //初始化赋值 count[0] = 1; count[1] = 1; for(int i = 2 ; i <= n ; i++) { int temp = (s[i-2] -'0')*10 + s[i-1] -'0'; if(s[i-1] != '0') { if(temp <= 26 && temp >=10) count[i] = count[i-1] + count[i-2]; else count[i] = count[i-1]; } //双0和0前数过大情况 else if( s[i-2] == '0' || s[i-2] > '2') { return 0; } else count[i] = count[i-2]; } return count[n]; }};
阅读全文
0 0
- LINTCODE——解码方法
- lintcode, 解码方法
- LintCode: 解码方法
- lintcode(512)解码方法
- 解码方法-LintCode
- lintcode----解码方法
- lintcode decode-ways 解码方法
- LintCode习题系列之解码方法
- lintcode——子树
- Lintcode——中位数
- Lintcode—跳跃游戏
- 最大间距—LintCode
- 快速幂—LintCode
- lintcode&九章算术——Lintcode No.80 中位数 ? 待解决
- 数组划分——LintCode
- 二分查找——LintCode
- 搜索区间——LintCode
- 更新二进制位——LintCode
- 不会玩Linux系统怎么找女朋友?
- spring连接池引入外部文件
- 字符串相关算法
- 内置对象:Session
- STM32移植到GD32的问题---20170923
- LINTCODE——解码方法
- 作业
- js中数据结构数组Array、映射Map、集合Set、对象、JSON
- Lucene简单的使用配置详解
- 数据结构 P22-24 算法实现 线性表的插入与删除
- Okhttp3使用 + 源码完全解析
- OkHttp工具类简单封装运用
- 反素数
- 使用XV-11激光雷达做hector_slam