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的更多相关文章

  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] 20. Valid Parentheses_Easy tag: Stack

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. LeetCode 394. 字符串解码(Decode String) 44

    394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...

  5. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

随机推荐

  1. 【cs229-Lecture4】Newton’s method

    之前我们在求Logistic回归时,用的是梯度上升算法,也就是要使得似然函数最大化,利用梯度上升算法,不断的迭代.这节课引出牛顿方法,它的作用和梯度上升算法的一样的,不同的是牛顿方法所需的迭代次数更少 ...

  2. SQL Server2008安装后1433端口没监听问题

    win2008系统安装完SQL Server2008后发现1433端口并没有监听,netstat -an并没有发现监听的1433端口,本机telnet localhost 1433也连不通,百度之后说 ...

  3. java基础---->数组的基础使用(二)

    这里对List(jdk 1.7)列表里面的一些方法做一些简单的分析,以避免有些函数的误用.手写瑶笺被雨淋,模糊点画费探寻,纵然灭却书中字,难灭情人一片心. List中注意的方法 一.Arrays.as ...

  4. MyBatis学习之多表查询

    一对多需求:即一张表class中又含有多张表(teacher,student)内容.现根据class_id 来获取对应的班级信息(包括学生和老师信息) 方式一:嵌套结果 使用嵌套结果映射来处理重复的联 ...

  5. Apache Server Status详解

    Apache的日志如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗.不过在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-statu ...

  6. css的border的solid

    http://www.w3school.com.cn/cssref/pr_border-style.asphttp://www.w3school.com.cn/tiy/t.asp?f=csse_bor ...

  7. iOS - 音乐播放器之怎么获取音乐列表

    方法一: 这个方法是通过获取到沙盒路径,来得到音乐的路径(使用这个方法需要把音乐放进沙盒) NSFileManager *manager = [NSFileManager defaultManager ...

  8. 9.10Django模板

    2018-9-10 16:37:29 模板就一个 不能嵌套 模板:  http://www.cnblogs.com/liwenzhou/p/7931828.html 2018-9-10 21:23:3 ...

  9. eclipse配置Js环境spket

    网上下载spket-1.6.16.jar破解版(目前最新版本) 1.如果你的JDK在1.6以上,可以直接双击spket-1.6.16.jar运行安装. 其它,使用命令行方式.(注意:自己切换命令行到s ...

  10. POJ-2081 Recaman's Sequence

    Recaman's Sequence Time Limit: 3000MS Memory Limit: 60000K Total Submissions: 22392 Accepted: 9614 D ...