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. 【Spring Boot&&Spring Cloud系列】Spring Boot中使用数据库之MySql

    对于传统关系型数据库来说,Spring Boot使用JPA(Java Persistence API)资源库提供持久化的标准规范,即将Java的普通对象通过对象关系映射(ORM)持久化到数据库中. 项 ...

  2. vue经验 - 实战疑点总结

    1.注册全局组件(是一个单vue页面组成的一个组件,而不是现拼的template结构) 结构: 代码:main.js import UserList from './components/UserLi ...

  3. scanf printf gets() puts(),cin cout

    最近在练机试题,常用的C和C++输入输出如下: 1 scanf 和printf int a; scanf("%d",&a) ; printf("%d", ...

  4. 题目1441:人见人爱 A ^ B(二分求幂)

    题目链接:http://ac.jobdu.com/problem.php?pid=1441 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. LeetCode 50 Pow(x, n) (实现幂运算)

    题目链接:https://leetcode.com/problems/powx-n/?tab=Description   Problem:实现幂运算即 pow(x,n)   设形式为pow(x,n)  ...

  6. 【读书笔记】socket函数

    socket函数 简介 应用程序调用socket函数来创建一个能够进行网络通信的套接字. 1 2 3 4 5 /* 头文件 */ #include <sys/types.h> #inclu ...

  7. 【咸鱼教程】Base64

    教程目录1 Base64简介2 使用Base643 Demo下载 1 Base64简介百度百科:Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045-RFC2 ...

  8. Mysql语句优化

    总结总结自己犯过的错,网上说的与自己的Mysql语句优化的想法. 1.查询数据库的语句的字段,尽量做到用多少写多少. 2.建索引,确保查询速度. 3.orm框架自带的方法会损耗一部分性能,这个性能应该 ...

  9. 调用office COM出现不会退出的问题

    症状 在使用.net调用 Microsoft Office 应用程序时,Office 应用程序在调用Quit方法时不会退出. 原因 Visual Studio.NET 从托管代码调用 COM 对象时, ...

  10. 利用n和nvm管理Node的版本

    写在前面 Node版本的迭代速度很快,版本很多(横跨0.6到0.11),升级Node版本成为了一个问题.目前有n和nvm这两个工具可以对Node进行无痛升级,本文简单介绍一下二者的使用. n n是No ...