LeetCode(91) Decode Ways
题目
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.
分析
这道题真是做的失败,竟然提交了5次才AC,一下拉低了AC率一大截,真是气煞~~~
究其原因还是判断条件考虑的不全面,在判断过程中不仅需要判断输入字符串的合法性(特别是当前字符为‘0’的时候)又要将字符串长度为1,2时单独处理~
不想说了,失落的贴上并不优美的代码,懒得修改~~~
AC代码
class Solution {
public:
int numDecodings(string s) {
if (s.empty())
return 0;
//求字符串长度
int len = s.length();
//记录对应长度字符串有几种表示方式
vector<int> ways(len + 1, 0);
for (int i = 0; i < len; ++i)
{
//对首位字符
if (i == 0)
{
//满足[1,9]
if ((s[0] - '0') > 0 && (s[0] - '0') <= 9)
{
ways[i] = 1;
continue;
}
else
return 0;
}
else{
//得到前一位
int tmp1 = s[i - 1] - '0';
//得到当前位
int tmp2 = s[i] - '0';
int tmp = tmp1 * 10 + tmp2;
//如果该两个字符可以表示为一个字母
if (tmp >= 10 && tmp <= 26)
{
//且当前处理为下标第2或以上字符
if (i > 1)
{
//当前位为0
if (tmp2 == 0)
ways[i] = ways[i - 2];
else
ways[i] = ways[i - 1] + ways[i - 2];
}
//此时处理为下标为0,1的两个字符
else
{
if (tmp2 == 0)
ways[i] = 1;
else
ways[i] = 2;
}
}
else{
if ((s[i] - '0') > 0 && (s[i] - '0') <= 9)
ways[i] = ways[i - 1];
else{
//此时代表字符串中间嵌入非法0,表示方式为0
return 0;
}
}
}
}//for
return ways[len-1];
}
};
LeetCode(91) Decode Ways的更多相关文章
- LeetCode(91):解码方法
Medium! 题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- HDU-1029-Ignatius aned the Princess IV
链接:https://vjudge.net/problem/HDU-1029#author=0 题意: 给你n个数字,请你找出出现至少(n+1)/2次的数字. 思路: dp,hash超时了,不知道是不 ...
- appium环境搭建思路
1.appium环境是不是需要appium的一个安装包? 2.我们针对android进行测试我们是不是需要android本身的一个android 的sdk? 3.android这个本身就是java基础 ...
- Python相对导入导致SystemError的解决方案(译)
原文出处: http://stackoverflow.com/ 译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现’System Error’的时候的解决方案.顺带一提, ...
- (转)Unity3D中常用的数据结构总结与分析
http://www.cnblogs.com/murongxiaopifu/p/4161648.html#array 1.几种常见的数据结构 常碰到的几种数据结构:Array,ArrayList, ...
- 03.Javascript——入门一些方法记录之Map和Set
JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串.但实际上Number ...
- 修改php默认的FastCGI模式为ISAPI模式的方法
一.到www.php.net中下载PHP的ZIP文件包.注意版本要对应. 二.将sapi目录中的:php4isapi.dll复制到c:\php目录中. 三.进入虚拟主机管理平台的"网站管理& ...
- gulp-htmlone的BUG弃坑
之前用项目用gulp-htmlone做最后的js和css内联打包出现各种问题 这次居然遇到打包后的css斜杠变反斜杠的问题 如下:/src/common/images/i_banner.jpg会被改成 ...
- 前端之CSS常见兼容性问题
1.双倍浮动BUG: 描述:块状元素设置了float属性后,又设置了横向的margin值,在IE6下显示的margin值要比设置的值大: 解决方案:给float的元素添加 display:inline ...
- CF1079D Barcelonian Distance
思路: 模拟. 实现: #include <bits/stdc++.h> using namespace std; ; double dis(double x1, double y1, d ...
- jQuery1.6.1源码分析系列(作者:nuysoft/高云)
作者:nuysoft/高云 QQ:47214707 Email:nuysoft@gmail.com jQuery源码分析(版本1.6.1) 00 前言开光 01 总体架构 02 正则表达式-RegEx ...