recursion solution, easy to understand


  • 0
    H

    personally, I think this solution is easier to be understood.
    ‘’‘
    class Solution(object):
    def decodeString(self, s):
    """
    :type s: str
    :rtype: str
    """
    queue = []
    time = 1
    isnum = False
    res = ''
    for i in xrange(len(s)):
    if s[i] == '[': queue.append(i)
    elif s[i] == ']':
    l = queue.pop()
    if not queue:
    res += time * self.decodeString(s[l+1:i])
    elif s[i].isnumeric():
    if not queue:
    time = int(s[i]) if (i==0 or not s[i-1].isnumeric()) else (10 * time + int(s[i]))
    else:
    if not queue:
    res += s[i]
    return res
    ‘’‘


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.