Decode Ways leetcode java
题目:
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[n+1]。dp[i]表示从1~i的decode ways的个数。
当给的code只有一位数时,判断是不是valid(A~Z),是的话就dp[1] = 1 不是的话就是dp[1] = 0
因为像给的例子12可以有两种可能的解析方法,所以计算dp[i]的时候要判断两种可能性,再累加。
代码如下:
- 1 public int numDecodings(String s) {
- 2 if (s.length()==0||s==null||s=="0")
- 3 return 0;
- 4
- 5 int[] dp = new int[s.length()+1];
- 6 dp[0] = 1;
- 7
- 8 if (isValid(s.substring(0,1)))
- 9 dp[1] = 1;
- else
- dp[1] = 0;
- for(int i=2; i<=s.length();i++){
- if (isValid(s.substring(i-1,i)))
- dp[i] += dp[i-1];
- if (isValid(s.substring(i-2,i)))
- dp[i] += dp[i-2];
- }
- return dp[s.length()];
- }
- public boolean isValid(String s){
- if (s.charAt(0)=='0')
- return false;
- int code = Integer.parseInt(s);
- return code>=1 && code<=26;
- }
Reference:
http://blog.csdn.net/u011095253/article/details/9248109
Decode Ways leetcode java的更多相关文章
- Decode Ways -- LeetCode
原题链接: http://oj.leetcode.com/problems/decode-ways/ 这道题要求解一个数字串依照字符串编码方式可解析方式的数量.看到这样的求数量的,我们非常easy想 ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 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 ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- leetcode面试准备:Decode Ways
1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 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 ...
随机推荐
- android 调试Installation failed with message INSTALL_FAILED_USER_RESTRICTED: Install canceled by user.
真机调试 遇到的问题 在使用真机调试的程序的时候出现了这个问题. 解决方法如下
- BZOJ4317: Atm的树
Description Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径都有边权,一个神秘的声音告诉他, ...
- 使用 IntraWeb (9) - JavaScript
IW 依赖 js 构建(我数了数, 在当前版本它的资源文件默认携带了 26 个 js); 但 IW 尽可能地让用户少用或不用 js, 但如果你对 js 也不陌生, IW 提供了多种途径与方便. 我给它 ...
- 【原创】Nginx+PHP-FPM优化技巧总结(转)
php-fpm的安装很简单,参见PHP(PHP-FPM)手动编译安装.下面主要讨论下如何提高Nginx+Php-fpm的性能. 1.Unix域Socket通信 之前简单介绍过Unix Doma ...
- .net core中的高效动态内存管理方案
.net core在新增的System.Buffers中引入了一大堆高效内存管理的类,如span和memory.内存池.本文今天这里介绍一个高效动态内存访问方案. ReadOnlySequenceSe ...
- PE Header and Export Table for Delphi
Malware Analysis Tutorial 8: PE Header and Export Table 2. Background Information of PE HeaderAny bi ...
- ARM-JTAG-SWD-schematic
- oracle定时任务的编写及查看删除
declare jobno number; begin dbms_job.submit( jobno,--定时器ID,系统自动获得 'PRC_INSERT;', --what执行的过程名 sysdat ...
- CF330 C. Purification 认真想后就成水题了
C. Purification time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 我所经历的SAP选型[转]
这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...