【LeetCode】394. Decode String 解题报告(Python)
作者: 负雪明烛
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)的更多相关文章
- [LeetCode] 394. Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】833. Find And Replace in String 解题报告(Python)
[LeetCode]833. Find And Replace in String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- Linux—export命令查看、修改用户环境变量
Linux export 命令用于设置或显示环境变量. 在 shell 中执行程序时,shell 会提供一组环境变量. export 可新增,修改或删除环境变量,供后续执行的程序使用. export ...
- SQL- case when then else end 用法经验总结
对case when 的理解总结: 1.then和else后,只能写一条输出语句且输出结果就是新生成列的值;when 后的条件判断可以有多条,且可以多个字段联合判断:end 后的输出也可以有多条,但必 ...
- APP工程师接入Telink Mesh流程 -3
加密是为了使网络更加的安全.健壮,若由于login.加密等流程 严重影响了 开发进程,也可以通过 修改SDK 固件 将login.加密 环节取消 1.发送数据.接受数据加密,解密去掉 mesh_sec ...
- kubernetes整个基础环境的准备
1.三台centos7,用CentOS-7-x86_64-Minimal-1708.iso安装的,记得统一选好时区,这三台会有etcd集群,其中一台做kubernetes服务端(也可以做三台服务端做负 ...
- Scrapy爬虫框架的安装和使用
Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...
- 学习Java的第十八天
一.今日收获 1.java完全学习手册第三章算法的3.1比较值 2.看哔哩哔哩上的教学视频 二.今日问题 1.在第一个最大值程序运行时经常报错. 2.哔哩哔哩教学视频的一些术语不太理解,还需要了解 三 ...
- 巩固javaweb的第二十八天
巩固内容: 设置页面的编码方式 实现代码: 每个 JSP 页面都需要设置编码方式,设置 JSP 页面的编码方式可以是下面两种方式 之一. 方式一: <%@ page contentType=&q ...
- 14. GLIBCXX_3.4.9' not found - 解决办法
在Linux中安装交叉编译器arm-linux-gcc 4.4.3,然后编译mini2440内核出错: /usr/lib/libstdc++.so.6: version GLIBCXX_3.4.9' ...
- InnoDB学习(二)之ChangeBuffer
ChangeBuffer是InnoDB缓存区的一种特殊的数据结构,当用户执行SQL对非唯一索引进行更改时,如果索引对应的数据页不在缓存中时,InnoDB不会直接加载磁盘数据到缓存数据页中,而是缓存对这 ...
- vim一键整理代码命令
vim下写代码超实用代码格式整理命令,仅需四步 ①先使用 gg 命令使光标回到第一行 ②shift+v 进入可视模式 ③shift+g 全选 ④按下 = 即可 混乱的代码格式 四步整理以后 工整又 ...