"Leetcode with Python" Decode Ways__python

Source: Internet
Author: User
Blog Domain name: Http://www.xnerv.wang
Original title page: https://oj.leetcode.com/problems/decode-ways/
Topic Type: Dynamic programming
Difficulty Evaluation: ★★★★
This article address: http://blog.csdn.net/nerv3x3/article/details/2921931

A containing letters from A-Z 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 "a", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "a" is 2.

DP problem, need to be noted in addition to the boundary conditions of the processing, as well as about ' 0 ' attention points. ' 0 ' itself can constitute a ' 10 ', ' 20 ' such a legitimate double-byte code, but the other case of ' 0 ' is abnormal data. This problem is the same as atoi, belong to the word game, the question itself does not say clearly to "as much as possible to decode the string", so at first I found that there are redundant ' 0 ' directly think that the decoding failure range 0, the results of the submission found wrong. In addition, the input parameter s may also be an empty string, from the result of the verification that this case should return 0, the title is not clear.
In addition to a large part of the DP problem, memory can be optimized, there is no need to use the n * 2 size two-dimensional array, just keep two lines, that is 2 * 2 of the space consumption can be.
Finally, you should also make a special note of a newly learned recruit. At first I also imagined the C language to write a swap function, with a temporary variable tmp to Exchange SOLU_ARR1 and SOLU_ARR2 two references, later found that can be used solu_arr1, solu_arr2 = SOLU_ARR2, solu_ Arr1 This simple wording, praise Python.

Class Solution:
    # @param s, a string
    # @return An integer
    def numdecodings (self, s):
        if None = s or 0 = Len (s) or ' 0 ' = s[0]: return
            0
        solu_arr1 = [0, 1]
        solu_arr2 = [0, 0]
        for i in range (1, Len (s) + 1):
            C  Ur_ch = s[i-1]
            last_ch = s[i-2] If 1!= i else '
            if cur_ch >= ' 1 ' and cur_ch <= ' 9 ':
                solu_arr2[0]  = Solu_arr1[0] + solu_arr1[1]
            else:
                solu_arr2[0] = 0
            double_ch = last_ch + cur_ch
            if ' = = Last_ch or ' 0 ' = = last_ch or int (double_ch) >:
                solu_arr2[1] = 0
            else:
                solu_arr2[1] = solu_arr1[0]
            solu_ar R1, solu_arr2 = solu_arr2, solu_arr1    # swap return

        solu_arr1[0] + solu_arr1[1]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.