题目如下:

解题思路:这种题目和四则运算,去括号的题目很类似。解法也差不多。

代码如下:

  1. class Solution(object):
  2. def decodeString(self, s):
  3. """
  4. :type s: str
  5. :rtype: str
  6. """
  7. stack = []
  8. for i in s:
  9. if i != ']':
  10. stack.append(i)
  11. continue
  12. repeatStr = ''
  13. while len(stack) > 0:
  14. v = stack.pop(-1)
  15. if v == '[':
  16. break
  17. repeatStr = v + repeatStr
  18. times = ''
  19. while len(stack) > 0:
  20. v = stack.pop(-1)
  21. if v == ']':
  22. break
  23. elif v == '[' or (v >= 'a' and v <= 'z'):
  24. stack.append(v)
  25. break
  26. times = v + times
  27. repeatStr *= int(times)
  28. stack += list(repeatStr)
  29. return ''.join(stack)

【leetcode】394. Decode String的更多相关文章

  1. 【LeetCode】394. Decode String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  2. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  4. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  5. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  6. 【LeetCode】91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  7. 【LeetCode】091. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  8. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

  9. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. C#和.NET获取绝对路径

    c#获取绝对路径:System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log.txt"); .net获取绝 ...

  2. leetcode 884. 两句话中的不常见单词 (python)

    给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不常用单 ...

  3. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_6_字符输出流写数据的其他方法

    从1开始写写三个字符 最后多了个bcd 写入字符串 字符串的一部分

  4. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_05 List集合_2_Arraylist集合

    数组查询快,增删慢. 不是同步的就是多线程的 ArrayList其实就是一个数组 这是add方法 它在添加元素的时候会创建新的数组,然后把元素复制过来.这就是为什么查询快,增删们的原因. 每次增加元素 ...

  5. tensorflow和keras的安装

    1 卸载tensorflow方法,在终端输入:  把protobuf删除了才能卸载干净. sudo pip uninstall protobuf sudo pip uninstall tensorfl ...

  6. LeetCode——141 设计链表

    题目: 简单说下思路: 用两个指针,一个跑得快,一个跑得慢(例如一个每次前进两步,一个前进一步),这样只要快指针不会撞上NULL(如果遇到了NULL的情况那么必然不存在环),快指针肯定会和慢指针碰面( ...

  7. spring,springMVC的优点和区别

    spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下◆目的:解决企业应用开发的复杂性◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能◆范围:任何Java应用简单 ...

  8. Far and away the best prize that life has given to us is the chance to work hard at work worth doing

    work at:侧重于某个工作场所,或者是工作领域内研究 work on:侧重于思想上的从事于某个工作. marvel:n.漫威.奇迹 means.n.方法 tailor.n.裁缝 brighten. ...

  9. SpringMVC起步(一)

    SpringMVC起步(一) 笔记来源于慕课网:https://www.imooc.com/video/7126/0 MVC:Model-View-Controller Model:模型层,业务数据的 ...

  10. python基础-4.1 open 打开文件练习:修改haproxy配置文件

    1.如何在线上环境优雅的修改配置文件? 配置文件名称ini global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 in ...