首页 > 其他分享 >【刷题笔记】91. Decode Ways

【刷题笔记】91. Decode Ways

时间:2023-10-21 22:06:19浏览次数:26  
标签:26 题目 数字 Ways 字母 Decode 字符串 91 dp

题目

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

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

题目大意

一条包含字母 A-Z 的消息通过以下方式进行了编码:

'A' -> 1
'B' -> 2
...
'Z' -> 26

给定一个只包含数字的非空字符串,请计算解码方法的总数。

解题思路

  • 给出一个数字字符串,题目要求把数字映射成 26 个字母,映射以后问有多少种可能的翻译方法。
  • 这题思路也是 DP。dp[n] 代表翻译长度为 n 个字符的字符串的方法总数。由于题目中的数字可能出现 0,0 不能翻译成任何字母,所以出现 0 要跳过。dp[0] 代表空字符串,只有一种翻译方法,dp[0] = 1。dp[1] 需要考虑原字符串是否是 0 开头的,如果是 0 开头的,dp[1] = 0,如果不是 0 开头的,dp[1] = 1。状态转移方程是 dp[i] += dp[i-1] (当 1 ≤ s[i-1 : i] ≤ 9);dp[i] += dp[i-2] (当 10 ≤ s[i-2 : i] ≤ 26)。最终结果是 dp[n]

参考代码

package leetcode

func numDecodings(s string) int {
	n := len(s)
	dp := make([]int, n+1)
	dp[0] = 1
	for i := 1; i <= n; i++ {
		if s[i-1] != '0' {
			dp[i] += dp[i-1]
		}
		if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
			dp[i] += dp[i-2]
		}
	}
	return dp[n]
}

标签:26,题目,数字,Ways,字母,Decode,字符串,91,dp
From: https://blog.51cto.com/u_16110811/7969613

相关文章

  • [915] Implementation of zooming to layer and exporting to PDF in arcpy
    ref:Camera-ArcGISProref:Introductiontoarcpy.mp#Setthepathtoyourprojectfile(.aprx)project_file=r"Map1.3Heritage.aprx"#Referencetheprojectaprx=arcpy.mp.ArcGISProject(project_file)#getthesitebufferlayerm=aprx......
  • ISO/ICE 9126软件质量模型真题
                ......
  • ISO/ICE 9126软件质量模型
          ......
  • [914] In Python's datetime library, you can format dates using the strftime() me
    InPython'sdatetimelibrary,youcanformatdatesusingthestrftime()method.Thismethodallowsyoutocreateaformattedstringrepresentationofadatetimeobject,specifyingtheformatyouwant.Here'showyoucanformatadateusingstrft......
  • [912] Airtable automation related stuff
    ref:Airtableautomationtrigger:Whenrecordcreatedref:HowtoexportdatafromAirtableThe"Whenrecordcreated"triggeristriggered whenever anewrecordiscreated.Thismeansthatifyouaremanuallyenteringdata,thistriggerwillli......
  • 无涯教程-NumPy - decode()函数
    此函数调用numpy.char.decode()解码给定的字符串。importnumpyasnpa=np.char.encode('hello','cp500')printaprintnp.char.decode(a,'cp500')其输出如下-�����hello参考链接https://www.learnfk.com/numpy/numpy-char-decode.html......
  • Oracle 中 decode 函数用法
    decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值)Decode函数与一系列嵌套的IF-THEN-ELSE语句相似。该函数的含义如下:IF条件=值1THENRETURN(翻译值1)ELSIF条件=值2THENRETURN(翻译值2)......ELSIF条件=值nTHENRETURN(翻译值n)......
  • [911] Read Data from Google Sheets into Pandas without the Google Sheets API (.g
    ref:ReadDatafromGoogleSheetsintoPandaswithouttheGoogleSheetsAPIimportpandasaspdsheet_id="1XqOtPkiE_Q0dfGSoyxrH730RkwrTczcRbDeJJpqRByQ"sheet_name="Sheet1"url=f"https://docs.google.com/spreadsheets/d/{sheet......
  • 软件测试|深入理解Python的encode()和decode()方法
    简介在Python中,字符串是不可变的序列对象,它由Unicode字符组成。当我们需要在字符串和字节之间进行转换时,Python提供了两个非常重要的方法:encode()和decode()。这两个方法允许我们在Unicode字符和字节之间进行相互转换,以便在处理文本和二进制数据时更加灵活。在本文中,我们将深入......
  • The solution of P9194
    10黑寄。problem&blog考虑到处理加边并不简单,所以我们可以考虑一个黑点\(p\),连边\((u,p)(p,v)\)。考虑在现在这棵树上连个点在原图中有变相连相当于有一个公共的\(p\)是它们的邻居。于是删边操作等价于将一个点的儿子黑点并到父亲黑点上。为了统计答案我们设\(x\)为......