Python Solution leetcode:394 Decode String

Source: Internet
Author: User

    • Title Description: According to the provisions, the string decoding, specific examples see the topic link

    • Idea: Use two stacks to store numbers and letters, respectively
    • Note 1: Numbers are multi-bit words, to be processed after entering the digital stack
    • Note 2: The combined string generated during the stack will continue into the letter stack
    • Note 3: Remember that when the alphabet is out of the stack, the characters are combined into strings.
    • Note 4: The reason for using the letter stack without strings is that the join efficiency of the string is higher than the string addition

    • Results: 98.02% MS, beat

    • Cons: Judging is the number there is a bit of code is not concise, you can move J to the outside of the loop

classSolution (Object):defDecodestring ( Self, s):""": Type S:str: Rtype:str        """Nums, chars=[], [] I, length= 0,Len(s) whileI<Length:j=I+ 1            ifS[i].isdigit (): num= int(S[i]) whileJ<LengthifS[j].isdigit (): num=Num* Ten + int(S[j]) J+= 1                    Else: BreakNums.append (num)elifS[i]== ' [' orS[i].isalpha (): Chars.append (S[i])Else: T, TMPC=Chars.pop (), [] whileT!= ' [': Tmpc.append (t) t=Chars.pop () TCHARs=Nums.pop ()* "'. Join (tmpc[::-1]) Chars.append (tchars) I=Jreturn "'. Join (Chars)

Python Solution leetcode:394 Decode String

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.