题目

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

  1. 'A' -> 1
  2. 'B' -> 2
  3. ...
  4. '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[n+1]。dp[i]表示从1~i的decode ways的个数。

当给的code只有一位数时,判断是不是valid(A~Z),是的话就dp[1] = 1 不是的话就是dp[1] = 0

因为像给的例子12可以有两种可能的解析方法,所以计算dp[i]的时候要判断两种可能性,再累加。

代码如下:

  1.  1     public int numDecodings(String s) {  
  2.  2         if (s.length()==0||s==null||s=="0") 
  3.  3             return 0; 
  4.  4 
  5.  5         int[] dp = new int[s.length()+1];  
  6.  6         dp[0] = 1;  
  7.  7         
  8.  8         if (isValid(s.substring(0,1)))
  9.  9             dp[1] = 1;  
  10.          else 
  11.              dp[1] = 0; 
  12.          
  13.          for(int i=2; i<=s.length();i++){  
  14.              if (isValid(s.substring(i-1,i)))  
  15.                  dp[i] += dp[i-1];  
  16.              if (isValid(s.substring(i-2,i)))  
  17.                  dp[i] += dp[i-2];  
  18.          }  
  19.          return dp[s.length()];  
  20.      }  
  21.        
  22.      public boolean isValid(String s){  
  23.          if (s.charAt(0)=='0') 
  24.              return false;  
  25.          int code = Integer.parseInt(s);  
  26.          return code>=1 && code<=26;  
  27.      } 

Reference:

http://blog.csdn.net/u011095253/article/details/9248109

Decode Ways leetcode java的更多相关文章

  1. Decode Ways -- LeetCode

    原题链接: http://oj.leetcode.com/problems/decode-ways/  这道题要求解一个数字串依照字符串编码方式可解析方式的数量.看到这样的求数量的,我们非常easy想 ...

  2. [LeetCode] Decode Ways 解码方法

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

  3. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

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

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

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

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

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

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

  7. leetcode面试准备:Decode Ways

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

  8. [LeetCode] Decode Ways II 解码方法之二

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

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

随机推荐

  1. android 调试Installation failed with message INSTALL_FAILED_USER_RESTRICTED: Install canceled by user.

    真机调试 遇到的问题 在使用真机调试的程序的时候出现了这个问题. 解决方法如下

  2. BZOJ4317: Atm的树

    Description Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径都有边权,一个神秘的声音告诉他, ...

  3. 使用 IntraWeb (9) - JavaScript

    IW 依赖 js 构建(我数了数, 在当前版本它的资源文件默认携带了 26 个 js); 但 IW 尽可能地让用户少用或不用 js, 但如果你对 js 也不陌生, IW 提供了多种途径与方便. 我给它 ...

  4. 【原创】Nginx+PHP-FPM优化技巧总结(转)

    php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能.   1.Unix域Socket通信   之前简单介绍过Unix Doma ...

  5. .net core中的高效动态内存管理方案

    .net core在新增的System.Buffers中引入了一大堆高效内存管理的类,如span和memory.内存池.本文今天这里介绍一个高效动态内存访问方案. ReadOnlySequenceSe ...

  6. PE Header and Export Table for Delphi

    Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...

  7. ARM-JTAG-SWD-schematic

  8. oracle定时任务的编写及查看删除

    declare jobno number; begin dbms_job.submit( jobno,--定时器ID,系统自动获得 'PRC_INSERT;', --what执行的过程名 sysdat ...

  9. CF330 C. Purification 认真想后就成水题了

    C. Purification time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  10. 我所经历的SAP选型[转]

    这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...