Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Decoding method of golang brushing leetcode skills

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report