91. Decode Ways
来源:互联网 发布:经典数据新闻案例分析 编辑:程序博客网 时间:2024/05/20 01:36
题目:
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.
代码:
class Solution {public: int numDecodings(string s) { int n=s.size(); if(n==0) return 0; vector<int> dp(n+1,0); dp[0]=1; for(int i=1;i<=n;i++){ if(s[i-1]!='0') dp[i]+=dp[i-1]; if(i>1&&s[i-2]=='1') dp[i]+=dp[i-2]; if(i>1&&s[i-2]=='2'&&s[i-1]>='0'&&s[i-1]<='6') dp[i]+=dp[i-2]; } return dp[n]; }};
笔记:
每一种情况对应dp[i]的一次叠加
阅读全文
0 0
- Leetcode 91. Decode Ways&&639.Decode ways
- [Leetcode] 91. Decode Ways
- LeetCode --- 91. Decode Ways
- [LeetCode]91.Decode Ways
- [leetcode] 91.Decode Ways
- 91. Decode Ways
- Algorithms-91.Decode Ways
- [leetcode] 91. Decode Ways
- 91. Decode Ways
- Leetcode 91. Decode Ways
- 91. Decode Ways
- leetcode 91. Decode Ways
- 91. Decode Ways
- 91. Decode Ways
- Leetcode 91. Decode Ways
- leetcode 91. Decode Ways
- LeetCode 91. Decode Ways
- 91. Decode Ways
- PHP框架
- Code,RO-data,RW-data,ZI-data
- 5296. 【清华集训2017模拟】Sequence 树套树
- windows和linux双系统安装
- 一些简单在开发中用到的封装函数
- 91. Decode Ways
- 怎么把种子文件里面的电影保存到网盘里面?
- 在Linux上,对于多进程,子进程继承了父进程的哪些
- 基础算法之“最近点对”
- Unreal4
- 重定向和转发的区别
- 35:字符串的展开
- 关于二分图的讲解
- 深入理解java异常处理机制