[LeetCode] 394. Decode String_Medium tag: stack 666
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". 这个题目用一个stack, 初始化为[["", 1]], 每次看到[, stack.append(["", num]), 看到], 将stack pop出来, 并且将st*k 加入到stack[-1][0], 最后返回stack[0][0], 太6666了!!! (2018/07/20)update:
这里注意一定要append[", 1"], 而不是tuple!!!! 因为tuple是不能被change的! 1. Constraints
1) 假设都有效, 并且不会有2[4]
2) 可以为empty 2. Ideas
stack T: O(n) S: O(n) 3. Code
class Solution:
def decodeString(self, s):
stack, num = [["", 1]], ""
for ch in s:
if ch.isdigit():
num += ch
elif ch == '[':
stack.append(["", int(num)])
num = ""
elif ch == ']':
st, k = stack.pop()
stack[-1][0] += st*k
else:
stack[-1][0] += ch
return stack[0][0]
4. Test cases
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
[LeetCode] 394. Decode String_Medium tag: stack 666的更多相关文章
- [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] 20. Valid Parentheses_Easy tag: Stack
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 394. 字符串解码(Decode String) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
随机推荐
- 百度前端学院js课堂作业合集+分析(更新中...)
第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 题目1162:I Wanna Go Home(最短路径问题进阶dijkstra算法))
题目链接:http://ac.jobdu.com/problem.php?pid=1162 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- hdu 4746Mophues[莫比乌斯反演]
Mophues Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327670/327670 K (Java/Others) Total ...
- 内网渗透中的NTLM-Hash Relay
基础知识 NTLN和Net-NTLM 1.NTLM(V1/V2)的hash是存放在安全账户管理(SAM)数据库以及域控的NTDS.dit数据库中,获取该Hash值可以直接进行PtH攻击,我博客中前文也 ...
- CONE NAT 和 Symmetric NAT
CONE NAT 和 Symmetric NAT 1. NAT 的划分 RFC3489 中将 NAT 的实现分为四大类: Full Cone NAT 完全锥形 NAT Restricted Cone ...
- PostgreSQL9.4如何指定数据库schema
在PostgreSQL中数据库可以有多个schema,在程序访问的时候如果不做特殊的设置,默认连接的是名为public的schema. 那么,如何设置能够让程序去访问特定的schema呢?之前在网上找 ...
- Maven属性(properties)标签的使用
在命令行使用属性时,是-D,比如:mvn -D input=test Properties 属性是了解POM基础知识的最后一个要素.Maven属性是值占位符,如Ant中的属性.它们的值可以通过使用符号 ...
- STM32 ADC转换时间
STM32F103XX的ADC的采样时钟最快14MHz,最快采样率为1MHz. ADC时钟: 这个ADC时钟是从哪来的呢.我们看下面这个STM32的时钟结构图: 我们大多使用STM32的最快PCLK2 ...
- CentOS7下Elastic Stack 5.0日志分析系统搭建
原文链接:http://www.2cto.com/net/201612/572296_3.html 在http://localhost:5601下新建索引页面输入“metricbeat-*”,之后ki ...
- 【SPOJ419】Transposing is Fun Pólya定理+欧拉函数
[SPOJ419]Transposing is Fun 题意:给你一个$2^a\times2^b$的矩阵,将$1...n$中的数依次从左到右,从上往下填到矩阵里,再把矩阵转置,然后把所有数从左到右,从 ...