Python 解LeetCode:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接
思路:使用两个栈分别存储数字和字母
注意1: 数字是多位的话,要处理后入数字栈
注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈
注意3: 记得字母出栈的时候字符要逆序组合成字符串
注意4: 不用字符串而用字母栈的原因是字符串的 join 效率会比字符串加法高一些
结果: 30 ms, beat 98.02%
缺点:判断是数字那里有点代码不简洁,可以把 j 挪到循环外面的
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
nums, chars = [], []
i, length = 0, len(s)
while i < length:
j = i + 1
if s[i].isdigit():
num = int(s[i])
while j < length:
if s[j].isdigit():
num = num * 10 + int(s[j])
j += 1
else:
break
nums.append(num)
elif s[i] == '[' or s[i].isalpha():
chars.append(s[i])
else:
t, tmpc = chars.pop(), []
while t != '[':
tmpc.append(t)
t = chars.pop()
tchars = nums.pop() * ''.join(tmpc[::-1])
chars.append(tchars)
i = j
return ''.join(chars)
Python 解LeetCode:394 Decode String的更多相关文章
- [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】394. Decode String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- Python 解leetcode:48. Rotate Image
题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
随机推荐
- [WEB安全]绕过URL跳转限制的思路
0x00 简介 说起URL跳转漏洞,有些人可能会觉得,不就是单纯的跳转到某一个其他网页吗?有什么用??? 给大家一个链接,你们进去看一下就明白了: http://www.anquan.us/searc ...
- iframe中用到的例子
<view:qrytr> <view:qrytd colspan="4"> <iframe id="ccbl&quo ...
- arts 打卡12周
一 算法: 字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找 ...
- openssl从内存中读取私钥进行签名
麻痹的找了好久,真恶心! #include <stdio.h> #include <stdlib.h> #ifdef WIN32 #include <windows.h& ...
- phpstorm 断点调试总是从index.php的第一行开始
去掉勾选,重开phpstorm
- C# 获取Windows 设备信息
namespace Beisen.Native { using Beisen.Pdf; using System; using System.Runtime.InteropServices; inte ...
- 完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题
①:例如很多人第一步就会这样做: 出现:Cannot download "https://github.com/sass/node-sass/releases/download/版本号/XX ...
- Oracle数据库查看表空间sql语句
转: Oracle数据库查看表空间sql语句 2018-09-03 15:49:51 兰海泽 阅读数 6212 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...
- Dart中排除空的情况:
但是dart的string类型还有另一个方法isNotEmpty,此时这样写: if (str?.isNotEmpty()) { // str is not empty, do something } ...
- Spring cloud微服务安全实战-3-6API安全机制之数据校验
校验:非空.唯一性等校验 密码的加密:密码加密来存储. 如何做https的访问 校验 一个层面是接口层面,另外一个层面是数据库层面. Springboot给我们提供了简单的封装 校验的包里面还有其他的 ...