[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 ...
随机推荐
- [Tjoi2016&Heoi2016]排序[01序列]
4552: [Tjoi2016&Heoi2016]排序 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 994 Solved: 546[Sub ...
- mvn deploy命令上传包
需求:有的时候需要单独上传release jar包,因为存在工程代码在A内网SVN,Nexus在B内网.这种情况下使用VPN也无法解决Jar包发布的问题. 这个时候采取的方式只能是: 打出jar包 - ...
- Particle 粒子效果使用(适配微信小游戏,particle is not defined)
在微信小游戏中使用粒子效果 参考: 1. 粒子库下载地址 2. 粒子官方使用教程 3. 水友解决微信小游戏particle is not defined 一.下载第三方库 Git地址:https:// ...
- 【BZOJ2138】stone Hall定理+线段树
[BZOJ2138]stone Description 话说Nan在海边等人,预计还要等上M分钟.为了打发时间,他玩起了石子.Nan搬来了N堆石子,编号为1到N,每堆包含Ai颗石子.每1分钟,Nan会 ...
- oracle简单存储过程以及如何查看编译错误
oracle简单存储过程以及如何查看编译错误; CREATE OR REPLACE PROCEDURE procedure_test ISval VARCHAR2(200);BEGIN /* val ...
- iOS - 使用WKWebView时OC调JS的user-select属性控制用户操作
// 页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigatio ...
- vue--todolist的实现
简单示例: <template> <div id="Home"> <v-header></v-header> <hr> ...
- 9.10Django模板
2018-9-10 16:37:29 模板就一个 不能嵌套 模板: http://www.cnblogs.com/liwenzhou/p/7931828.html 2018-9-10 21:23:3 ...
- 7.18 python进程间数据共享
# 管道# 数据共享 Manager# 进程池和回调函数 ! # !/usr/bin/env python # !--*--coding:utf-8 --*-- # !@Time :2018/7/18 ...
- 基本类型互相之间转化可以用Covent类来实现。
一.C#类型的转换 在c#中类型的转换分两种:显式和隐式,基本的规则如下: 1.基类对象转化为子类对象,必须显式转换,规则:(类型名) 对象.2.值类型和引用类型的转换采用装箱(boxing)或拆箱( ...