Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The Decoding method of golang brushing leetcode skills

Shulou Source: shulou.com Published: 2022-06-02 00:15:55 11月14日 Update

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]

Tags: Methods situations questions techniques code more examples help answers explanations input output easy easy content dynamic variables letters guys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB vpn Redmi Xiaomi Shulou Tech Info