91. Decode Ways java solutions

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 即可。

但不同的是本题有多种限制:
第一: s[i-1]不能是0,如果s[i-1]是0的话,num[i]就只能等于num[i-2]
第二,s[i-2,i-1]中的第一个字符不能是0,而且Integer.parseInt(s.substring(i-2,i))获得的整数必须在0到27之间

 1 public class Solution {
 2     public int numDecodings(String s) {
 3         if(s == null || s.length() == 0) return 0;
 4         int[] num = new int[s.length()+1];
 5         num[0] = 1;
 6         num[1] = s.charAt(0) != ‘0‘ ? 1:0;
 7         for(int i = 2; i <= s.length(); i++){
 8             if(s.charAt(i-1) != ‘0‘)
 9                 num[i] = num[i-1];
10             if(s.charAt(i-2) != ‘0‘ && Integer.parseInt(s.substring(i-2,i)) < 27)
11                 num[i] += num[i-2];
12         }
13         return num[s.length()];
14     }
15 }
时间: 2024-08-17 05:41:59

91. Decode Ways java solutions的相关文章

[LeetCode] 91. Decode Ways 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 enco

[email&#160;protected] [91] Decode Ways (Dynamic Programming)

https://leetcode.com/problems/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 wa

[LeetCode]91.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 encode

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

Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给一串包含数字的加密报文,求有多少种解码方式 举个例子,已知报文"12",它可以解码为AB(1 2),也可以是L (12) 所以解码方式有2种. 测试样例 Input: "0" "121212" "1010

Leet Code OJ 91. Decode Ways [Difficulty: Medium]

题目: 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 enc

[LeetCode] 91. 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

91. 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 enc

【Leetcode】91. Decode Ways

Yesterday, Bro Luo told me: "Why don't you improve your English by writing your blogs in English?" I think it may be a good way and i did so today. Problem Description: A message containing letters from A - Z  is being encoded to numbers using t

[leetcode DP]91. 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