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

思路: 一个DFS的题目, 给定的字符串可能会有嵌套很多层, 在每一层我们只要在碰到正常的字符就保存到当前层的结果中, 如果碰到数字就另外保存起来作为倍数, 碰到'[' 就进入下层递归, 碰到']' 就将当前层结果返回, 这样在返回给上层之后就可以用倍数加入到上层结果字符串中. 最终当所有层都完成之后就可以得到结果. 在不同层次的递归中, 我们可以维护一个共同的位置索引, 这样在下层递归完成之后上层可以知道已经运算到哪里了.

DFS的思想:

DFS算法
思想:一直往深处走,直到找到解或者走不下去为止
DFS(dep,...) // dep代表目前DFS的深度
{
if (找到解或者走不下去了){
return;
}
枚举下种情况,DFS(dep + , ...)
} DFS: 使用栈保存未被检测的节点,结点按照深度优先的次序被访问并依次压入栈中,并以相反的次序出栈进行新的检测
类似于树的先根遍历,深搜的例子:走迷宫,没有办法用分身术来站在每一个走过的位置。
例子:
leetcode class Solution {
public:
string DFS(string s, int &k){ // 在所有的层中是维护一个共同的索引值k
string ans;
int cnt = ;
while(k < s.size())
{
if(isdigit(s[k])){ // 判断是否是数字
cnt = cnt * + s[k++]-''; // cnt的变化值为什么这样乘以10
}
else if(s[k]=='['){ // 如果是左括号,则要进入下层递归
string tem = DFS(s, ++k); // 递归调用
for(int i = ; i < cnt; i++){
ans += tem;
}
cnt = ; //
}
else if(s[k]==']'){
k++;
return ans;
}
else ans += s[k++]; // 若是普通数字就直接加上
}
return ans;
} string decodeString(string s){
string res;
int k = ;
return DFS(s, k);
}
};
 

Leetcode -- 394. Decode String的更多相关文章

  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 解题报告(Python)

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

  3. Python 解LeetCode:394 Decode String

    题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...

  4. 【leetcode】394. Decode String

    题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...

  5. 394 Decode String 字符串解码

    给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...

  6. 394. Decode String

    [题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...

  7. 394. Decode String 解码icc字符串3[i2[c]]

    [抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...

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

  9. LC 394. Decode String

    问题描述 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], wh ...

随机推荐

  1. VS错误:#error 指令: Please use the /MD switch for _AFXDLL builds

    我在做MFC时遇到过这个问题,解决方法如下: 修改设置:工程(Project)-> 属性(Properties)-> 配置属性(Configuration Properties)-> ...

  2. Windows服务器防火墙配置规范

    本文属于一篇内部规范文档,整理的初衷是为了规范.统一集团的Windows服务器(仅仅SQL Server数据库服务器)防火墙设置,仅仅供内部其它同事设置Windows防火墙时作为参考的文档资料.如有不 ...

  3. [20180904]工作中一个错误.txt

    [20180904]工作中一个错误.txt --//昨天看我提交一份修改建议,发现自己写的sql语句存在错误.--//链接:http://blog.itpub.net/267265/viewspace ...

  4. 转自:stuff字符串拼接方法

    下文讲述数据表中将多列合并到一列的方法分享 转自:http://www.maomao365.com/?p=6796

  5. MSSQL 如何采用sql语句 获取建表字段说明、字段备注、字段类型、字段长度

    转自: http://www.maomao365.com/?p=4983 <span style="color:red;font-weight:bold;">下文讲述- ...

  6. Linux CFS调度器之虚拟时钟vruntime与调度延迟--Linux进程的管理与调度(二十六)

    1 虚拟运行时间(今日内容提醒) 1.1 虚拟运行时间的引入 CFS为了实现公平,必须惩罚当前正在运行的进程,以使那些正在等待的进程下次被调度. 具体实现时,CFS通过每个进程的虚拟运行时间(vrun ...

  7. 利用python搭建Powersploit powershell脚本站点

    powershell脚本站点的搭建 一.Powersploit Powersploit是一款基于powershell的后渗透(Post-Exploitation)框架,集成大量渗透相关模块和功能. 下 ...

  8. excel数据表透视操作

    虽然作为开发人员,很少用到office,但是在工作的时候,特别是做财务模块,或多或少都会用到excel处理数据,对比数据.比如说vlookup函数,数据透视表这些.vlookup函数我用得很熟练,但数 ...

  9. linux命令应用之一

    某个目录下有两个文件a.txt和b.txt.文件格式为(ip username),例如: a.txt 127.0.0.1 zhangsan127.0.0.1 wangxiao127.0.0.2 lis ...

  10. 为什么黑客都不用鼠标?你听说过Linux吗?

    为什么黑客都不用鼠标?你听说过Linux吗? 微软和它的朋友们的如意算盘. 下面来看看微软的收入是怎么来的.首先,Windows系列操作系统,一个就是 800+RMB,每次升级又是几乎同样的价钱.Wi ...