题目

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. Codeforces 1139D(推式子+dp)

    题目传送 推公式博客传送 推完式子就是去朴素地求就行了Orz const int maxn = 1e5 + 5; const int mod = 1e9 + 7; int m, mu[maxn], v ...

  2. bzoj2818: Gcd懵逼乌斯反演

    由于是单组数据,强行不分块O(n)过 线性筛部分非常神奇,用了一个奇妙的推导(懒得写了) #include <bits/stdc++.h> using namespace std; ],f ...

  3. 通过API获取统计信息时报Access denied错误处理记录

    通过API获取HDFS统计信息时报Access denied错误信息,错误信息如下: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.s ...

  4. 下面给出了四种设计模式的作用: 外观(F

    下面给出了四种设计模式的作用: 外观(Fa?ade :为子系统中的一组功能调用提供一个一致的接口,这个接口使得这一子系统更加容易使用: 装饰(Decorate):当不能采用生成子类的方法进行扩充时,动 ...

  5. Log4j日志框架小记

    人啊,总是在学习中发现不足,不足中学习,学习中成长. 今天来系统记录一下对于常用日志组件的理解.配置.使用. 仅供参考, 错误之处请各路好汉不吝笔墨批评指正. 转载请注明出处 Log4j日志框架是Ap ...

  6. git更新到远程服务器代码

    git commit -a 在vi里输入一些内容 wq退出,git pull, git push

  7. leetcode287 Find the Duplicate Number

    思路: 转换成链表之后使用floyed判环法.转换之后重复的那个数字是唯一一个有多个前驱和一个后继的节点,因此是环的起始节点. 实现: class Solution { public: int fin ...

  8. Eclipse-运行符-数据类型转换-环境变量配置

    1.能够使用Eclipse快捷键 ctrl + /   单行注释:再按一次则取消: ctrl + shift + /  多行注释:  ctrl + shift + \  取消多行注释: ctrl + ...

  9. Android 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置

    前几天看到了有人在android5.0上实现了如下图一样的效果,我自己就去搜了下.参考了国外一篇文章和国内的一篇文章,最终实现了想要的效果.具体参考的网址我已经贴到文章末尾,大家可以去英文的那个网站看 ...

  10. Spring Boot :Druid Monitor

    忙里偷个闲,在这里分享一下SpringBoot集成Druid实现数据库监控功能,有什么错误欢迎大家指出! 参考文件: Spring实现Druid监控:https://www.cnblogs.com/w ...