The Decoding method of golang brushing leetcode skills
Golang leetcode skills decoding method, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
A message containing the letter Amurz is encoded in the following ways:
'A'-> 1
'B'-> 2
...
'Z'-> 26
Given a non-empty string that contains only numbers, calculate the total number of decoding methods.
Example 1:
Enter: "12"
Output: 2
Explanation: it can be decoded as "AB" (12) or "L" (12).
Example 2:
Enter: "226"
Output: 3
Explanation: it can be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (22 6)
Ideas for solving the problem:
1. Dynamic programming solution
Suppose s [0: I Mel 1] has a dp [I] decoding scheme.
2, state transfer equation
A, if s [I] ='0', there are two cases
(1) s [I-1] ='1' | |'2'
At this time, s [I-1] s [I] must be decoded together.
Therefore, dp [I-1] = DP [I-1]
(2) other circumstances
At this time, decoding failed.
Dp [iTun1] = 0
B, it can be decoded alone or together with s [I-1] if s [I-1] = '1century recording s [I]
Dp [iTun1] = DP [I] + DP [I-1]
C, if s [I-1] = '2mathematics] s [I] >' 0' & & s [I]