- 浏览: 188430 次
- 性别:
- 来自: 济南
-
文章分类
最新评论
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数组,题目中要求只有数字1 - 26才是合法数字,当出现‘0’的时候就无法解码。因此我们可以首先判断一下字符串的第一个字符是否为‘0’,如果为‘0’我们可以直接返回0,当然如果字符串本身为空,也是直接返回0。如果不为0,这时我们让dp[0] = 1,对应了第一个字符只有一种解码方式, 对于到第i个字符时,如果它不为‘0’,它可能的情况至少有dp[i - 1]种,然后我们查看第i个字符和第i- 1 个字符构成的数字是否在1-26之间,如果在,那么到第i个字符总共有dp[i - 1] + dp[i - 2]种,这时(i > 1, i == 1 的时候 有dp[i - 1] + 1 种) ,这样递推式就有了,代码如下:
'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数组,题目中要求只有数字1 - 26才是合法数字,当出现‘0’的时候就无法解码。因此我们可以首先判断一下字符串的第一个字符是否为‘0’,如果为‘0’我们可以直接返回0,当然如果字符串本身为空,也是直接返回0。如果不为0,这时我们让dp[0] = 1,对应了第一个字符只有一种解码方式, 对于到第i个字符时,如果它不为‘0’,它可能的情况至少有dp[i - 1]种,然后我们查看第i个字符和第i- 1 个字符构成的数字是否在1-26之间,如果在,那么到第i个字符总共有dp[i - 1] + dp[i - 2]种,这时(i > 1, i == 1 的时候 有dp[i - 1] + 1 种) ,这样递推式就有了,代码如下:
public class Solution { public int numDecodings(String s) { if(s == null || s.length() == 0 || s.charAt(0) == '0') return 0; int[] dp = new int[s.length()]; dp[0] = 1; for(int i = 1; i < s.length(); i++) { if(s.charAt(i) != '0') dp[i] = dp[i - 1]; if(s.charAt(i - 1) != '0') { int code = Integer.parseInt(s.substring(i - 1, i + 1)); if(code >= 1 && code <= 26) { if(i > 1) { dp[i] += dp[i - 2]; } else { dp[i] += 1; } } } } return dp[s.length() - 1]; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 288Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 293You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 413Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 396Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 521Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 600Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 503Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 696Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 495The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 450Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 611Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 625Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 451All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 924Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 950Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 626Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 715Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 897Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 809You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 749For a undirected graph with tre ...
相关推荐
Decode Ways II是普通Decode Ways问题的变种,其难度被提升到了困难级别,它允许数字0-25之间的任意映射,而不仅仅是1-26,这导致解题的边界条件和逻辑更加复杂。 在给出的Java代码中,首先需要理解问题的具体要求...
Decode Ways问题是一个典型的动态规划问题,也称为“解码方法”。该问题描述如下:给定一个只包含数字的字符串,需要通过以下规则将数字编码为字母:'1' 对应 'A','2' 对应 'B',依此类推,'26' 对应 'Z'。同时,...
本压缩包文件名为“c语言-leetcode题解之0091-decode-ways.zip”,它聚焦于解决LeetCode上的一个特定问题——“0091. Decode Ways”。这个题目的核心是关于字符串解码,要求编写一个C语言函数,能够将输入的一个仅...
"DEcode Ways"是LeetCode中的第91题,它涉及到字符串处理和动态规划的算法知识。下面将详细讨论这个题目以及如何使用JavaScript来解决它。 解题思路: 问题描述:给定一个只包含大写字母的非空字符串s,已知每个...
在探讨如何解决LeetCode上的“Decode Ways”这一问题时,我们首先需要理解题目的基本要求。这个问题要求我们给出一个从1到26的编码系统,其中每个数字代表一个字母,但26代表'Z'。给定一个数字串,我们的任务是计算...
java java_91-decode-ways
例如,著名的“解码方法”问题(Decode Ways)就要求计算给定的数字编码有多少种可能的解码方式,这需要对动态规划有深入的理解。 在解决LeetCode的解码问题时,关键在于理解编码的逻辑,然后选择合适的数据结构和...
Ways](./leetcode/动态规划-Decode Ways.java) [动态规划-Distinct Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid ...
在探讨JavaScript中解决LeetCode上编号为91的“解码方法数”的问题时,我们首先需要了解该问题的具体内容和相关的算法背景。编号91的问题是:一个仅包含数字的字符串能否被解码为字母组合,这里的解码规则是:'1'...
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 ...Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
加油站 leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167....2107/03/06 15.3 总和,16.3 ...91.Decode Ways, 96.Unique Binary Search Tree, 120.Triangle, 139.Word Break, 152.Maximum Produ
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 ...我经常在递归的结束地方忘记return!...091:Decode Ways 简单的一维DP,用额外数组O(n)即可。 139,1
扩展矩阵leetcode interview-algorithm leetCode 待解决 上楼梯问题 how many ways to decode this message @leetCode 91
input_text = "Artificial intelligence is changing the world in ways we can't even imagine." generated_text = generate_text(input_text) print("Generated text:", generated_text) ``` 这会返回一个包含...
There’s basically two ways to measure this: same-bitrate (e.g. a 500kbps VP8 file vs. a 500kbps VP9 file, where the VP9 file likely looks much better), or same-quality (e.g. a VP8 file with SSIM=...
1997 - 2003 Sergio A....and fixed it. Some minor bugs that I don‘t remember fixed.- Added MIME-compliant base64 support (not for use by now). Added examples.0.9.2.1b- Fixed a bug when send a mail and ...