作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/decode-string/description/

题目描述

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

题目大意

对字符串进行解码,字符串的编码方式是数字+[字符串] == > 字符串连续重复数字次。求最后解码的字符串是多少。

解题方法

看到括号匹配的题肯定想到用栈去做。

这个题,遇到’[‘就把之前的字符串进行进栈操作。遇到’]'进行出栈操作。

curstring保存的是出栈操作完成后的字符串。

注意这一步:curstring = prestring + prenum * curstring,prestring是前面的字符串,prenum * curstring是这一步骤结束之后的字符串,所以是前面的字符串+现在的字符串得到目前已有的字符串。

class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
curnum = 0
curstring = ''
stack = []
for char in s:
if char == '[':
stack.append(curstring)
stack.append(curnum)
curstring = ''
curnum = 0
elif char == ']':
prenum = stack.pop()
prestring = stack.pop()
curstring = prestring + prenum * curstring
elif char.isdigit():
curnum = curnum * 10 + int(char)
else:
curstring += char
return curstring

日期

2018 年 2 月 17 日
2019 年 1 月 2 日 —— 2019年开刷

【LeetCode】394. Decode String 解题报告(Python)的更多相关文章

  1. [LeetCode] 394. Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  2. Leetcode -- 394. Decode String

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  3. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  4. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  5. 【LeetCode】833. Find And Replace in String 解题报告(Python)

    [LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...

  6. 【LeetCode】678. Valid Parenthesis String 解题报告(Python)

    [LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

随机推荐

  1. Linux— 查看系统的位数

    [root@zf-test-web01-4 ~]# file /bin/ls           #"/bin/ls" is a binary file /bin/ls: ELF ...

  2. 在VS2008环境下编写C语言DLL,并在C++和C#项目下调用 (转载)

    1.编写DLL a)文件--打开--新建项目--Win32,右侧Win32项目,填写好项目名称,点击"下一步", 应用程序类型选择:"DLL(D)",附加选项: ...

  3. tensorboard No dashboards are active for the current data set.

    修改一下启动命令时的路径 位置示例: 命令为   E:\PYTHON_PROJECT\testTF\inceptionV1_net\log>tensorboard --logdir=TEC4FN ...

  4. 全网最详细的ReentrantReadWriteLock源码剖析(万字长文)

    碎碎念) 花了两天时间,终于把ReentrantReadWriteLock(读写锁)解析做完了.之前钻研过AQS(AbstractQueuedSynchronizer)的源码,弄懂读写锁也没有想象中那 ...

  5. 【Reverse】初遇花指令

    解密花指令 全文参考了一个大师傅的blog:https://blog.csdn.net/zhangmiaoping23/article/details/38400393 介绍 花指令是对抗反汇编的有效 ...

  6. 【Reverse】每日必逆0x03

    BUU-刮开有奖 附件:https://files.buuoj.cn/files/abe6e2152471e1e1cbd9e5c0cae95d29/8f80610b-8701-4c7f-ad60-63 ...

  7. 【Linux】【Services】【Docker】网络

    容器的网络模型: closed container: 仅有一个接口:loopback 不参与网络通信,仅适用于无须网络通信的应用场景,例如备份.程序调试等: --net none bridged co ...

  8. 如何利用EL表达式获取list,map,对象等值

    <%@ page import="com.hopetesting.domain.User" %><%@ page import="java.util.* ...

  9. 解决在进行socket通信时,一端输出流OutputStream不关闭,另一端输入流就接收不到数据

    输出的数据需要达到一定的量才会向另一端输出,所以在传输数据的末端添加 \r\n 可以保证不管数据量是多少,都立刻传输到另一端.

  10. array_filter()用法

    第一种情况: 通过函数,过滤数组中的元素 array_filter($arr,'函数名称') 函数里可以写相应的过滤原则,下面举个栗子,过滤掉不是数字的元素 $arr=array('a','b','c ...