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

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
class Solution {
public int numDecodings(String s) {
int[] arr = new int[s.length() + 1];
arr[0] = 1;
// for case of '0';
arr[1] = s.charAt(0) == '0' ? 0 : 1;
for (int i = 2; i <= s.length(); i++) {
int preOne = Integer.valueOf(s.substring(i - 1, i));
int preTwo = Integer.valueOf(s.substring(i - 2, i));
if (preOne >= 1 && preOne <= 9) {
arr[i] += arr[i - 1];
}
if (preTwo >= 10 && preTwo <= 26) {
arr[i] += arr[i - 2];
}
}
return arr[s.length()];
}
}

[LC] 91. Decode Ways的更多相关文章

  1. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  2. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

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

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

  4. 91. Decode Ways

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

  5. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  6. [LeetCode] 91. Decode Ways 解码方法

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

  7. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

  8. leetcode 91 Decode Ways ----- java

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

  9. 【LeetCode】91. Decode Ways

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

随机推荐

  1. python刷LeetCode:7. 整数反转

    难度等级:简单 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 3: ...

  2. svg用例

    圆<circle cx="x" cy="y" r="r" style="stroke:black;fill:none&quo ...

  3. webview Java与JS互调

    Android调用JS:方法一 webView.loadUrl("javascript:show('"+info+"')"); Android调用JS:方法二 ...

  4. Java搭建WebSocket的两种方式

    下面分别介绍搭建方法:一.直接使用Java EE的api进行搭建.一共3个步骤:1.添加依赖<dependency>    <groupId>javax</groupId ...

  5. 面向对象 part4 构造函数对象重写

    出处 其中深奥之处非看一次能了解 !对象真的有点绕,但是又很严谨

  6. deque & list

    deque 双向队列 它也是采用动态数组的方式来管理的提供了随机数组 和vector的区别 1.deque头尾两端可以开放,能够进行快速的插入和删除(vector只能在尾部进行快速的插入和删除) 2. ...

  7. 数据处理——One-Hot Encoding

    一.One-Hot Encoding     One-Hot编码,又称为一位有效编码,主要是采用位状态寄存器来对个状态进行编码,每个状态都由他独立的寄存器位,并且在任意时候只有一位有效.     在实 ...

  8. 怎么通过scanf读取一个空白前的字符

    /************************************************************************* > File Name: scanf2.c ...

  9. mysql创建某个数据库中的某张表 只读用户

    1.创建用户,并授权SELECT查询权限,授权远程访问权限,注意,命令中username/password指用户名密码,请自己指定.若要限制仅指定IP可以使用此用户访问Mysql,将%改为具IP即可, ...

  10. 【转】高频使用的git清单

    侵删 作者: 阮一峰 链接: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 我每天使用 Git ,但是很多命令记不住. 一般来 ...