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. [CISCN2019 华北赛区 Day2 Web1]Hack World

    知识点:题目已经告知列名和表明为flag,接下来利用ascii和substr函数即可进行bool盲注 eg: id=(ascii(substr((select(flag)from(flag)),1,1 ...

  2. Django模板渲染——(二)

    模板标签 模板是由HTML代码和一些逻辑控制代码组成的,逻辑控制代码除了前面介绍的变量和过滤器,还要一个非常重要的模板标签.模板标签的语法规则是{% tag %},模板标签在渲染的过程中能提供任意的逻 ...

  3. atan2&sin

    //弧度转化角度:弧度*180/PI //角度转化弧度:角度*PI/180 #include <math.h> #include <stdio.h> const double ...

  4. Python笔记_第四篇_高阶编程_正则表达式_2.正则表达式入门

    1. 匹配单个字符和数字: . --->> 匹配除换行符以外的任意字符.[0123456789] --->> []字符集合,表示匹配方括号中所包含的任意一个字符.[Thomas ...

  5. 实体机安装Ubuntu系统

    今天windows突然蓝屏了,索性安装个 Ubuntu 吧,这次就总结一下实体机安装 Ubuntu 的具体步骤 note: 本人实体机为笔记本 型号为:小米pro U盘为金士顿:8G 安装系统:Ubu ...

  6. ZJNU 1426 - YNingC的困惑

    注意到N最大只有1e6,但是xy最大有2e8,直接模拟2e8会超时 所以可以将1e6个区间离散化后模拟,模拟时的最坏情况为2e6满足题意 /* Written By StelaYuri */ #inc ...

  7. classification.py

    # -*- coding: utf-8 -*-"""View more, visit my tutorial page: https://morvanzhou.githu ...

  8. Linux-使用syslog来记录调试信息

    1.有三个函数:openlog.syslog.closelog 2.一般的log信息都在操作系统的/var/log/messages这个文件中存储着,但是ubuntu中是在/var/log/syslo ...

  9. 常用的模型集成方法介绍:bagging、boosting 、stacking

    本文介绍了集成学习的各种概念,并给出了一些必要的关键信息,以便读者能很好地理解和使用相关方法,并且能够在有需要的时候设计出合适的解决方案. 本文将讨论一些众所周知的概念,如自助法.自助聚合(baggi ...

  10. iOS 加急审核的办法

    前言:由于自己的APP在提交后,审核了大概一周左右还没有消息,而领导又不断询问情况,于是自己在网上看到了这篇文章.由于自己比较懒,所以在此记录下来,以供 大家参考. 说明:本文只是做一个记录,还望看到 ...