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 ...
随机推荐
- 剑指offer部分编程题
一 斐波那契数列 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 问题分析: 可以肯定的是这一题通过递归的方式是肯定能做出来,但是这样会有 ...
- shell与aw的简单交互示例
先来看一段shell代码 word=“a test name” selectword=“test"lineitem=`echo "$word" | awk 'match( ...
- .Net 第一章笔记
1.深入.NET框架 对象数组 登录和注册 内存级别数据的拎取 1..NET 战略 Java领域:::::SQL Server不会用到 浏览器IE 口号:任何人 在任何地方 使用任何终端,,都可以使用 ...
- STL使用迭代器逆向删除
网上有很多这种例子: void erase(vector<int> &v) { for(vector<int>::reverse_iterator ri=v.rbegi ...
- spark性能测试理论-Benchmark(转)
一.Benchmark简介Benchmark是一个评价方式,在整个计算机领域有着长期的应用.正如维基百科上的解释“As computer architecture advanced, it becam ...
- webstorm使用总结
1.webstorm显示ES6语法错误,和nodejs语法错误未提示的问题,只需要在 此处解决ES6语法错误问题: 此处解决不支持node语法的问题: 然后就显示正常啦.
- linux 删除文件后空间没有释放的解决办法
清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem ...
- Maven常见知识介绍
1)pom详解 2)pom详解 3)测试 4)插件与生命周期 5)maven生命周期 6)范围依赖
- shiro : java.lang.IllegalArgumentException: Odd number of characters.
shiro使用的时候: java.lang.IllegalArgumentException: Odd number of characters. at org.apache.shiro.cod ...
- myeclipse报错MA
以下问题萌新问了我很多次了,无奈写个随笔.之后问的我都在这个随笔里补充. 断电/自动关机导致的问题: Could not open the editor: the file does not exis ...