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. ssh 账号密码登录设置

    找到/etc/ssh/sshd_config文件中的 PasswordAuthentication no 改为PasswordAuthentication yes 并保存. 重启ssh服务:sudo ...

  2. MySQL--基础SQL--DDL

    1.创建数据库 CREATE DATABASE dbname 例: CREATE DATABASE test 2.选择要操作的数据库 USE dbname 例: USE test 3.删除数据库 DR ...

  3. 修复grub

    进入命令行模式,#chroot /mnt/sysimage :切换根目录#grub2-install /dev/sda :安装grub2到第一硬盘#grub2-mkconfig -o /boot/gr ...

  4. 【MySQL参数】-innodb_flush_method

    innodb_flush_method这个参数控制着innodb数据文件及redo log的打开.刷写模式,对于这个参数,文档上是这样描述的:有三个值:fdatasync(默认),O_DSYNC,O_ ...

  5. PAT Vocabulary

    数字 单词 词性 中文意 常用 音标 non-negative 非负数 non-negative number Positive 正整数 Positive integer Negative 负数 Ne ...

  6. UML-如何画SSD?

    1.SSD来自哪里?答:用例文本 2.如何为系统事件和操作命名? 3.SSD中的哪些需要放到词汇表中? SSD元素包含 1).操作名称 2).参数 3).返回数据 这些元素,必须要简洁.但别人可能不太 ...

  7. 循环队列--忘记分配空间和如何用tag判断队空队满

    #include<iostream> #define maxsize 100 using namespace std; struct CLqueue { int *Q; int front ...

  8. Window RabbitMq安装

    rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 Erlang 实现的工业级的消息队列(MQ)服务器,Rab ...

  9. python获取当前时间戳

    import time # 获取当前时间戳print(int(time.time()))

  10. Codeforces1304D Shortest and Longest LIS

    前置扯淡 %%@\(wucstido\),思路是在是巧妙---link Description 给一个长度为\(n\)由 \(<\) 和 \(>\)组成的字符串,表示序列中相邻位置的数的大 ...