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.

做了好久才AC,主要是脑子不清楚,思路不清晰。虽然最后想通了但花了一整个下午。

思路:

f(n) 表示,从第n个字符起的字符串可以排列的方式

从后向前判断,如果该数字是0,则只有0种方式;如果数字非0,则该位数字单独解码,方式有f(n - 1)种。如果该数字可以和它后面的数字一起解码,则该数字和他后面的数字一起,方式再加上f(n-2)种。

class Solution {
public:
int numDecodings(string s) {
int iway[] = {,};
int slen = s.length();
if(slen <= )
return ;
iway[] = (s[slen - ] == '') ? : ; for(int i = s.length() - ; i >= ; i--)
{
int curWays = ;
if(s[i] != '')
{
curWays += iway[];
}
int num = (s[i] - '') * + (s[i + ] - '');
if( <= num && num <= )
{
curWays += iway[];
}
iway[] = iway[];
iway[] = curWays;
}
return iway[];
}
};

大神更加简洁的代码,思路差不多,就是从前向后判断的:

int numDecodings(string s) {
// empty string or leading zero means no way
if (!s.size() || s.front() == '') return ; // r1 and r2 store ways of the last and the last of the last
int r1 = , r2 = ; for (int i = ; i < s.size(); i++) {
// zero voids ways of the last because zero cannot be used separately
if (s[i] == '') r1 = ; // possible two-digit letter, so new r1 is sum of both while new r2 is the old r1
if (s[i - ] == '' || s[i - ] == '' && s[i] <= '') {
r1 = r2 + r1;
r2 = r1 - r2;
} // one-digit letter, no new way added
else {
r2 = r1;
}
} return r1;
}

【leetcode】Decode Ways(medium)的更多相关文章

  1. 【leetcode】Decode Ways

    题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...

  2. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  3. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  5. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  6. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  7. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  8. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  9. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

随机推荐

  1. Stm32 debug停留在"BKPT 0xAB"或者"SWI 0xAB"的解决办法。

    一..背景: 曾经在工作中接触过STM32一段时间,但没有深入的去学习,只是用前辈搭建好的模型来实现一些功能罢了,俗话说的好,大树底下好乘凉,开发确实轻松了,可是不深究点,又觉着心里不踏实,然而也一直 ...

  2. hdu.1111.Secret Code(dfs + 秦九韶算法)

    Secret Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  3. 初始Jquery--以及工厂函数

    一.JavaScript框架 1什么是JavaScript框架 普通JavaScript的缺点:每种控件的操作方式不统一,不同浏览器下有区别,要编写跨浏览器的程序非常麻烦.因此出现了很多对JavaSc ...

  4. Ubuntu 下apache2开启rewrite隐藏index.php

    为了实现 http://www.example.com/route/route 而不是 http://www.example.com/index.php/route/route 需要开启apache2 ...

  5. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  6. HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...

  7. HDU 1086You can Solve a Geometry Problem too(判断两条选段是否有交点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 判断两条线段是否有交点,我用的是跨立实验法: 两条线段分别是A1到B1,A2到B2,很显然,如果 ...

  8. iOS开发——UI基础-UIScrollView

    一.UIScrollView使用的步骤 1.创建UIScrollView 2.将需要展示的内容添加到UIScrollView中 3.设置UIScrollView的滚动范围 (contentSize) ...

  9. UOJ#35 —— 后缀排序

    1.题目大意:后缀数组模板题 2.分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,''也要加入排序的 存个模板QAQ #include <cstdio> #include & ...

  10. lintcode 447 Search in a Big Sorted Array

    Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...