题目

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];
}
};

GitHub测试程序源码

LeetCode(91) Decode Ways的更多相关文章

  1. LeetCode(91):解码方法

    Medium! 题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计 ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. 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 ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. laravel-admin 配置富文本编辑器流程

    laravel-admin默认去除富文本编辑器的,官方也给出了配置方法. 我配置的是wangEditor,本来配置完后就能愉快得使用了,可万万没想到还是有坑的.默认是用base64上传的,也就是数据库 ...

  2. JSDOM获取子节点的一些方法

    一般情况获取子节点,通过找到查找父节点的ID或者class类名,来获取父节点,再通过children属性,得到子节点的数组: 之前在另外一篇随笔中说过,如果使用另一个属性childNode,会把注释. ...

  3. python入门之实例-用户登录、注册

    用户密码存储文件db(其中用户和密码之间用$符合隔开): admin$123456 root$sdfk9f24 chy$654321 代码如下: def login(username,password ...

  4. Spark Mllib里数据集如何取前M行(图文详解)

    不多说,直接上干货! 见具体, Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第13章 使用决策树二元分类算法来预测分类StumbleUpon数据集 见具体 Hadoop+Spark大 ...

  5. java 并发容器一之BoundedConcurrentHashMap(基于JDK1.8)

    最近开始学习java并发容器,以补充自己在并发方面的知识,从源码上进行.如有不正确之处,还请各位大神批评指正. 前言: 本人个人理解,看一个类的源码要先从构造器入手,然后再看方法.下面看Bounded ...

  6. wine使用

    wineqq 不能输入问题winecfg在 wine 设置里,选择函数库添加 riched20, 就行了(原装领先于内建) wineqq 可以输入不能输入中文问题原因:fictx组件缺失 搜狗输入法没 ...

  7. Idea注释参数报错,控制台乱码问题解决方法

    idea虽然工具非常好用,但是他的一些解决方法网上非常的少,有些压根没有,解决这些问题非常浪费时间 1.最近在工作中发现一个问题,使用ant打包后,控制台总是报错,提示信息还是乱码的,吓得我赶紧用回了 ...

  8. 了解springcloud

    spring cloud比较不错的文章 https://blog.csdn.net/zhaozhenzuo/article/details/52803490?utm_source=blogxgwz9 ...

  9. jquery的load方法

    load方法指定一个界面会显示在目标的标签内部 比如MVC的一个分部视图页面想要显示在某个标签里面,可以写成 $(标签ID).load(分部视图名称,data) 其中第二个参数可选,主要是一些需要传递 ...

  10. aspose.cell 给excel表格设置样式

    方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...