Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码:
'A' -> 1 'B' -> 2 ... 'Z' -> 26
给定一个只包含数字的非空字符串,请计算解码方法的总数。
示例 1:
输入: "12" 输出: 2 解释: 它可以解码为 "AB"(1 2)或者 "L"(12)。
示例 2:
输入: "226" 输出: 3 解释: 它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
class Solution {
public:
int numDecodings(string s)
{
int len = s.size();
if(len == 1)
return s[0] == '0'? 0 : 1;
if(s[0] == '0')
return 0;
vector<int> dp(len, 0);
dp[0] = 1;
int x = (s[0] - '0') * 10 + s[1] - '0';
if(s[1] == '0' && s[0] <= '2' && s[0] >= '1')
dp[1] = 1;
else if(s[1] == '0' && s[0] > '2')
return 0;
else if(x <= 26 && x >= 1)
{
dp[1] = 2;
}
else
dp[1] = 1;
for(int i = 2; i < len; i++)
{
int x = (s[i - 1] - '0') * 10 + s[i] - '0';
if(x == 0)
return 0;
else if(s[i - 1] == '0')
{
dp[i] = dp[i - 1];
}
else if(s[i] == '0' && s[i - 1] <= '2' && s[i - 1] >= '1')
{
dp[i] = dp[i - 2];
}
else if(s[i] == '0' && s[i - 1] > '2')
{
return 0;
}
else if(x <= 26 && x >= 1)
{
dp[i] = dp[i - 1] + dp[i - 2];
}
else if(x > 26)
dp[i] = dp[i - 1];
}
return dp[len - 1];
}
};
Leetcode91.Decode Ways解码方法的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 【leetcode-91 动态规划】 解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
随机推荐
- Git婴幼儿使用手册【十分钟让你帅气的使用命令行和团队工作】
Git由来:...... Git使用的好处:...... 如何使用Git:(以上会显得我们以下的是很纯纯的干货) 代码库有两个部分: 本地代码库:远程代码库: 本地代码库使用方法: 一.先创建一个文件 ...
- OpenCASCADE圆与平面求交
OpenCASCADE圆与平面求交 eryar@163.com 在 解析几何求交之圆与二次曲面中分析了OpenCASCADE提供的类IntAna_IntConicQuad可以用来计算圆与二次曲面之间的 ...
- day 39 MySQL之多表查询
MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都 ...
- Jmeter接口测试(第二篇)
一.新建项目 1.运行Jmeter.bat打开Jmeter 2.添加线程组(测试计划->添加->Thread(users)->线程组) 3.添加HTTP请求(线程组->添加-& ...
- vue:父子组件间通信,父组件调用子组件方法进行校验子组件的表单
参考: ElementUI多个子组件表单的校验管理:https://www.jianshu.com/p/541d8b18cf95 Vue 子组件调用父组件方法总结:https://juejin.im/ ...
- [转]WPF更换主题
如果要做到一个应用程序其基本的内容不变,但改变整个应用程序的外观可以这样做: 对于每一套外观定义一个ResourceDictionary 在应用程序中,动态加载此应用程序(或窗体)的Resource ...
- innodb状态
Innodb_buffer_pool_pages_data Innodb buffer pool缓存池中包含数据的页的数目,包括脏页.单位是page. Innodb_buffer_pool_pages ...
- Laravel 错误处理
错误提示:cURL error 60: SSL certificate problem: unable to get local issuer certificate 解决方案:修改文件,重启队列即可 ...
- 开源代码分析-react-native-eyepetizer
目录结构: app----imgs --- pages ------ home ------ explore ------ follow ------ profile ------ selected ...
- opencv4 java投影
工程下载 https://download.csdn.net/download/qq_16596909/11505994 比较适合与验证码的处理,毕竟八邻域降噪不能消除比较大的噪点,为了尽量减少噪点对 ...