Description

A message containing letters from A-Z can be encoded into numbers using the following mapping:

‘A’ -> “1”
‘B’ -> “2”

‘Z’ -> “26”

To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, “11106” can be mapped into:

  • “AAJF” with the grouping (1 1 10 6)
  • “KJF” with the grouping (11 10 6)
    Note that the grouping (1 11 06) is invalid because “06” cannot be mapped into ‘F’ since “6” is different from “06”.

Given a string s containing only digits, return the number of ways to decode it.

The answer is guaranteed to fit in a 32-bit integer.

Examples

Example 1:

Input: s = “12”
Output: 2
Explanation: “12” could be decoded as “AB” (1 2) or “L” (12).

Example 2:

Input: s = “226”
Output: 3
Explanation: “226” could be decoded as “BZ” (2 26), “VF” (22 6), or “BBF” (2 2 6).

Example 3:

Input: s = “0”
Output: 0
Explanation: There is no character that is mapped to a number starting with 0.
The only valid mappings with 0 are ‘J’ -> “10” and ‘T’ -> “20”, neither of which start with 0.
Hence, there are no valid ways to decode this since all digits need to be mapped.

Example 4:

Input: s = “06”
Output: 0
Explanation: “06” cannot be mapped to “F” because of the leading zero (“6” is different from “06”).

Constraints:

1 <= s.length <= 100
s contains only digits and may contain leading zero(s).

思路

是动态规划的思路
假设现在有一串数字

1 2 2 3

初始化112所可能做成的count数目

1 2 2 3
1(“1”) 2(“1”“2”/“12”)

对于第三个数字2来说,他能组成的数字是count(“1”) + "22"以及count(“12”) + "2"两种情况。也就是说,假设"112"的count初始为0:

  • 如果"22"能成功被解码,则count+=count(“1”)
  • 如果"2"能成功被解码,则count+=count(“12”)

那么对于该例子来说

1 2 2 3
1(“1”) 2(“1”“2”/“12”) 3 = 1(count(“1”)[“22”=true]) + 2(count(“12”)[“2”=true])

再去看最后一位数

1 2 2 3
1 2 3 5=2(count(“12”)[“23”=true]) + 3(count(“122”)[“3”=true])

最后明确一下初始情况就可以了
几个需要注意的初始样例

“10”
“301”
“27”

代码

class Solution {public int numDecodings(String s) {if(s.length() == 0){return 0;}if(s.charAt(0) == '0'){return 0;}if(s.length() == 1){return 1;}int first = 1;int second = 2;if(s.charAt(1) == '0' || Integer.parseInt(s.substring(0, 2)) > 26){second = 1;}if(s.charAt(1) == '0' && Integer.parseInt(s.substring(0, 2)) > 26){second = 0;}if(s.length() == 2){return second;}for(int i = 2; i < s.length(); i++){int count = 0;if(Integer.parseInt(s.substring(i - 1, i + 1)) < 27 && s.charAt(i - 1) != '0'){count += first;}   if(s.charAt(i) != '0'){count += second;}first = second;second = count;}return second;}
}

# 91 Decode Ways相关推荐

  1. 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways

    LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...

  2. [LeetCode]91.Decode Ways

    题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...

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

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

  4. 91 Decode Ways

    91 Decode Ways dp解法 O(1) space class Solution:# @param {string} s# @return {integer}def numDecodings ...

  5. 91 Decode Ways

    为什么我常常对做题产生恐惧,因为可能为了一个不算难的问题不知不觉绕进去2个小时,这显然是不值得的.这题就是如此. 还要注意,java && 的优先级高于||的优先级,而不是同级. pu ...

  6. 【leetcode】91. Decode Ways A-Z的字母表示1-26的数字,反向破解多少种字符串的可能性...

    1. 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  7. LeetCode 91 Decode Ways(编码方式)(*)

    原文 A message containing letters from A-Z is being encoded to numbers the following mapping: 'A' -> ...

  8. Leet Code OJ 91. Decode Ways [Difficulty: Medium]

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  9. LeetCode 91. Decode Ways

    问题链接 LeetCode 91 题目解析 A~Z对应数字1~26,给出一段数字串,求破译方法数. 解题思路 动态规划.关键在于分类,定义 \(dp[i]\) 为前i个字符的解密方法数,初始化为0. ...

最新文章

  1. Windows xp下配置Apache、PHP环境及Oracle10g客户端
  2. 服务器不显示内存条,服务器检测不到内存条
  3. Android开发手册 (Android的手工教程MtAndroid开发手册)
  4. Spring声明式事务管理示例——MyBatis学习笔记之十六
  5. TCP/IP的全部IP协议号
  6. java 接口类型_Java-从接口类型而不是类声明
  7. 计算机毕业设计-网上购书系统【代码讲解+安装调试+文档指导】
  8. mysql端口被占用了如何解决_如何解决Win10安装MYSQL端口被占用?
  9. android手机安装win10,安卓手机成功安装运行桌面版win10系统
  10. 谈谈百度/GOOGLE联盟和一般联盟的区别
  11. WinRAR分卷压缩与解压缩
  12. QT程序退出后托盘图标不消失问题
  13. 模拟IC设计——简单放大器的直流仿真
  14. Carrot保卫萝卜
  15. 福建师范大学 “挑战杯”校赛金银奖分析文档
  16. 高通 android笔记本电脑,联想将推高端安卓平板 搭载高通骁龙870处理器
  17. Delphi最新版电子体温单源码(免控件)
  18. Lesson 25 Do the English speak English? 英国人讲的是英语吗?
  19. 【GDOI2016模拟4.23】飞机调度
  20. 物流仓储管理中超高频RFID的应用

热门文章

  1. 论文精读 ——《BEVDepth: Acquisition of Reliable Depth for Multi-view 3D Object Detection》
  2. php 时间转时辰,Powershell小技巧之获取当前的时间并转换为时辰
  3. 【瞎逛】音乐碎碎笔记 01
  4. 【七里香】雨下整夜 我的爱溢出就像雨水
  5. 一个印度人写的VC串口类CSerialCom(有串口基础介绍)
  6. 世界工厂不好当了 东莞面临新一轮企业倒闭潮
  7. Cadence Allegro导出BOM清单图文教程及视频演示
  8. oracle中的latch: cache buffers chains 与热块
  9. freertos---队列管理
  10. 《C++ Templates》笔记 Chapter 12 Fundamentals in Depth-Chapter 13 Names in Templates