作者: 负雪明烛
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. confluence——实现

    基于CentOS6.9 [root@localhost ~]# yum install mysql mysql-server -y [root@localhost ~]#yum install -y ...

  2. lxml解析库的安装和使用

    一.lxml的安装lxml是Python的一个解析库,支持HTML和XML的解析,支持XPath解析方式,而且解析效率非常高.本节中,我们了解一下lxml的安装方式,这主要从Windows.Linux ...

  3. R2CNN模型——用于文本目标检测的模型

    引言 R2CNN全称Rotational Region CNN,是一个针对斜框文本检测的CNN模型,原型是Faster R-CNN,paper中的模型主要针对文本检测,调整后也可用于航拍图像的检测中去 ...

  4. C语言之内核中的struct list_head 结构体

    以下地址文章解释很好 http://blog.chinaunix.net/uid-27122224-id-3277511.html 对下面的结构体分析 1 struct person 2 { 3 in ...

  5. A Child's History of England.35

    The other two clung to the yard for some hours. At length the young noble said faintly, 'I am exhaus ...

  6. Flink(三)【核心编程】

    目录 一.Environment 二.Source 从集合读取数据 从文件读取数据 从kakfa读取数据(常用) 自定义数据源 三.Transform map Rich版本函数 flatMap key ...

  7. Cocoapods 版本更新与更新到指定版本

    1.本地现有的Cocoapods的版本号是1.1.0.rc.2,想升级到最新版本 1.先切换gem源 gem sources --remove https://rubygems.org/ gem so ...

  8. Linux基础命令---lftp登录ftp服务器

    lftp lftp指令可以用来登录远程ftp服务器,这是一个字符界面的文件传输工具. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. ...

  9. 【JS】枚举类型

    https://zhuanlan.zhihu.com/p/79137838 相当于用数字来代替一串字母 /** * 时间:2019年8月18日 * 前端教程: https://www.pipipi.n ...

  10. df和du显示不同

    目录 一.简介 二.原因分析 三.解决方法 一.简介 Linux服务器,使用df -h查看文件系统使用率,可以看到/dev/xvdb1磁盘占用了约27G,挂载目录为/opt. 但进入到opt目录中执行 ...