Java for LeetCode 091 Decode Ways
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的更多相关文章
- [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 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 ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- 【LeetCode】091. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- [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
首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...
- [LeetCode]题解(python):091 Decode Ways
题目来源 https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encod ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- cocos2dx 2.x新建项目
举例: cocos2d-x-2.2.6/tools/project-creator 进入 这个文件夹 chmod 777 project-cereator.py ./create_project.py ...
- leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法
Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space charact ...
- ofstream的使用方法
ofstream的使用方法ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C 中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包 ...
- GraphMatrix::BFS广度优先搜索
查找某一结点的邻居: virtual int firstNbr(int i) { return nextNbr(i, n); } //首个邻接顶点 virtual int nextNbr(int i, ...
- vue 中 this.$router.push() 路由跳转传参 及 参数接收的方法
传递参数的方法:1.Params 由于动态路由也是传递params的,所以在 this.$router.push() 方法中 path不能和params一起使用,否则params将无效.需要用name ...
- -ROOT-表和.META.表结构详解
在<HBase技术简介>中我们知道,HBase中有两个特殊的表:-ROOT-和.META.. 由于HBase中的表可能非常大,故HBase会将表按行分成多个region,然后分配到多台Re ...
- [转]如何用C++实现一个LRU Cache
[转自http://hawstein.com/posts/lru-cache-impl.html] LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法 ...
- uva 11885 - Number of Battlefields(矩阵高速幂)
题目连接:uva 11885 - Number of Battlefields 题目大意:给出周长p,问多少种形状的周长为p的,而且该图形的最小包围矩阵的周长也是p,不包含矩形. 解题思路:矩阵高速幂 ...
- java获取网页源码
获取网页的源码: package com.atguigu.crud.controller; import java.io.BufferedReader; import java.io.Buffered ...
- mycat可以干什么
单纯的读写分离,此时配置最为简单,支持读写分离,主从切换 分表分库,对于超过 1000 万的表进行分片,最大支持 1000 亿的单表分片 多租户应用,每个应用一个库,但应用程序只连接 Myca ...