leetcode hot100 字符串解码

394. 字符串解码

已解答

中等

相关标签

相关企业

给定一个经过编码的字符串,返回它解码后的字符串。

编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。

你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。

此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。

class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """

        # 先得到对应关系
        hashmap_end={}
        stack=[]
        for index,str_t in enumerate(s):
            if str_t=='[':
                stack.append(index)
                hashmap_end[index]=-1
            elif str_t==']':
                end = stack[-1]
                hashmap_end[end]=index
                stack.pop()
            



        hashmap = {}
        for i in range(10):
            hashmap[str(i)]=i

        # print(hashmap_end)
        # print(hashmap)
        def func_t(start,end):
            i=start
            rt_str=""
            repeat_num=0
            while i<end:
        
                if hashmap.get(s[i])==None and s[i]!='[' and s[i]!=']':
                    rt_str+=s[i]
                    i+=1
                elif hashmap.get(s[i])!=None:
                    # 读取数字然后递归调用
                    repeat_num = repeat_num*10 + int(s[i])
                    i+=1
                elif s[i]=='[':
                    # reapeat = int(repreat_num)
                    # stack.append(i)
                    tmp = func_t(i+1,hashmap_end[i])
                    rt_str = rt_str + tmp*repeat_num
                    i = hashmap_end[i]+1
                    repeat_num=0
                else:
                    print(s[i])

            
            return rt_str  
        return func_t(0,len(s))





上述是我自己的解法,主要解决的是括号里面还有括号的情况,就用迭代的方法解决,每次都返回括号内的输出结果,在呈上次数,缺点是需要提前的把括号的前后index准备好。

class Solution(object):
    def decodeString(self, s):
        """
        :type s: str
        :rtype: str
        """
        
        def dfs(i):
            multi = 0
            res=""
            while i< len(s):
                if s[i]=="[":
                    i,tmp = dfs(i+1)
                    res = res + multi*tmp
                    multi=0
                elif  s[i]>='0' and s[i]<='9':
                    multi = 10*multi +int(s[i])
                    i+=1
                    
                elif s[i]==']':
                    
                    return i+1 , res
                else:
                    res+=s[i]
                    i+=1
            return res
        return dfs(0)







这个是标准代码,直接把所有的迭代放到一起了

class Solution:
    def decodeString(self, s: str) -> str:
        # 递归的办法
        # 看到[就开始递归了
        # 
        # 遍历一遍然后得到每一个括号对应的位置
        stack = []
        hashmap={}
        for index , i in enumerate(s):
            if i=='[':
                stack.append(('[',index))
            if i==']':
                x,indexx = stack.pop()
                # print(indexx)
                hashmap[indexx] = index
        self.hashmap=hashmap
        print(hashmap)
        return self.subdecode(s,0)
    def subdecode(self,s,start):
        print(s,start)
        if not s or s=='' :
            return ''
        n=0
        res = ''
        
        for index , i in enumerate(s):
            
            # index = start+index
            if i>='a' and i<='z':
                res+=i
            if i>='0' and i<='9' :
                if n==0:
                    n=int(i)
                else:
                    n = 10*n + int(i) 
            if i=='[':
                # print(index)
                return res  + n * self.subdecode(s[index+1:self.hashmap[index+start]-start],index+start+1) + self.subdecode(s[self.hashmap[index+start]-start+1:],self.hashmap[index+start]+1)
        return res




        # stack = []
        # n=0
        # last_res = ''
        # res = ''
        # for i in s:
        #     if i>='0' and i<='9':
        #         n=int(i)
        #     if i=='[':
        #         stack.append((n,res))
        #         n=0
        #         res = ''
        #     if i>='a' and i<='z':
        #         res +=i
        #     if i==']':
        #         multi,last_res = stack.pop()
        #         res =  last_res + res*multi

        # return res
        # 分来那个黄总情况,1.没有嵌套括号的情况下,记录了倍数,经过括号之后吧res变成应该的值,然后经过数字的时候把前面的res记录成lastres放到栈里面去,然后清空multi和res继续做
        # 2.有嵌套的情况下,一样,这是人能想出来的吗

        

再次添加了栈的方法,感觉根本不是人可以想出来的。还是自己的方法好用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值