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.

解题思路:

dp问题,JAVA实现如下:

		static public int numDecodings(String s) {
if (s.length() == 0 || s.charAt(0) == '0')
return 0;
int[] dp = new int[s.length() + 1];
dp[0] = dp[1] = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '0'&& (s.charAt(i - 1) != '1' && s.charAt(i - 1) != '2'))
return 0;
else if (s.charAt(i) == '0')
dp[i + 1] = dp[i - 1];
else if (s.charAt(i - 1) == '0'||(s.charAt(i-1)-'0')*10+(s.charAt(i)-'0')>26)
dp[i + 1] = dp[i];
else
dp[i + 1] = dp[i - 1] + dp[i];
}
return dp[s.length()];
}

Java for LeetCode 091 Decode Ways的更多相关文章

  1. [LeetCode] 639. Decode Ways II 解码方法 II

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

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

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

  3. 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 ...

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

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

  5. 【LeetCode】091. 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 639 Decode Ways II

    首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...

  8. [LeetCode]题解(python):091 Decode Ways

    题目来源 https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encod ...

  9. leetcode 91 Decode Ways ----- java

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

随机推荐

  1. Autolayout 02

    Working with Auto Layout Programmatically 如果你在运行阶段添加或者移除views你就需要通过代码来添加约束来保证你的interface能正确适应size或者o ...

  2. Start Developing iOS Apps Today

    view types - view常见类型

  3. linux df -h卡成狗

    linux执行df -h命令,直接卡在,很久没有任何动静 strace df -h跟踪命令执行,查看卡在那里 还有可能就是重做系统吧,检测下内存条是不是坏了等 http://blog.51cto.co ...

  4. Exception:System.Threading.SemaphoreFullException

    ylbtech-Error-Exception-C#: System.Threading.SemaphoreFullException    1.A,异常类型返回顶部 1,异常名称System.Thr ...

  5. C# 的概念

    1,C#-ASP.NET C# 的概念 2,Intro ASP.NET 一,基本概念: 1,C#--语言 microsoft 开发的纯面向对象的语言,是VS2005的主流开发语言. 语言的发展 C-- ...

  6. django网站搭建常用的一些代码

    from functools import wrapsdef check_user_login(func): @wraps(func) def return_wrapper(request, *arg ...

  7. 【前端GUI】—— 前端设计稿切图通用性标准

    前言:公司在前端组和视觉组交接设计稿切图的时候,总会因为视觉组同事们对前端的实现原理不清楚而出现各种问题,在用的时候还得再次返工,前端组同事们一致觉得应该出一份<设计稿切图通用性标准文件> ...

  8. 导出excel文件工具类

    package com.rrz.common.utils.excel; import java.io.IOException;import java.io.OutputStream;import ja ...

  9. Vue 内容分发slot

    1.概述 内容分发:混合父组件的内容与子组件自己的模板. 2.单个插槽 当子组件模板只有一个没有属性的插槽时,父组件传入的整个内容片段将插入到插槽所在的 DOM 位置,并替换掉插槽标签本身. 最初在  ...

  10. java:可变参数(转载)

    http://12477787.blog.51cto.com/12467787/1887843 Java在1.5之后允许方法使用可变参数,可变参数的好处在于:它允许传递0个或者多个参数.比如原来有一段 ...