Leetcode -- 394. Decode String
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的更多相关文章
- [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:394 Decode String
题目描述:按照规定,把字符串解码,具体示例见题目链接 思路:使用两个栈分别存储数字和字母 注意1: 数字是多位的话,要处理后入数字栈 注意2: 出栈时过程中产生的组合后的字符串要继续入字母栈 注意3: ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- 394 Decode String 字符串解码
给定一个经过编码的字符串,返回它解码后的字符串.编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正整数.你可以认 ...
- 394. Decode String
[题目] Total Accepted: 10087 Total Submissions: 25510 Difficulty: Medium Contributors: Admin Given an ...
- 394. Decode String 解码icc字符串3[i2[c]]
[抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...
- [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 ...
- LC 394. Decode String
问题描述 Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], wh ...
随机推荐
- MySQL 如何查看表的存储引擎
MySQL 如何查看表的存储引擎 在MySQL中如何查看单个表的存储引擎? 如何查看整个数据库有那些表是某个特殊存储引擎,例如MyISAM存储引擎呢?下面简单的整理一下这方面的知识点. 如果要查看 ...
- MySQL 5.7忘记root密码如何修改?
一直以来,MySQL的应用和学习环境都是MySQL 5.6和之前的版本,也没有去关注新版本MySQL 5.7的变化和新特性.今天帮人处理忘记root密码的时时候,发现以前的方法不奏效了.具体情况如下所 ...
- mysql练习----More JOIN operations
This tutorial introduces the notion of a join. The database consists of three tables movie , actor a ...
- shell编程—简单的使用(二)
使用shell编辑.sh使其输出hello tynam 1.新建一个.sh文件,然后进行编辑 vi hello_tynam.sh 2.进行编辑,先按i键进行激活,然后输入echo hello tyna ...
- timeout 命令
命令简介 运行指定的命令,如果在指定时间后仍在运行,则杀死该进程.用来控制程序运行的时间. 使用方法 1 2 3 timeout [选项] 数字[后缀] 命令 [参数]... 后缀 s 代表秒(默认值 ...
- c/c++ 友元的简单应用
友元的简单应用 1,对象 + 对象,或者,对象 + 数字,可以用类的成员函数去重载+号函数,但是,数字 + 对象就不能用类的成员函数去重载+号函数了, 因为编译器会把数字 + 对象翻译成数字.oper ...
- oracle- 数据表分区
1. 表分区概念 分区表是将大表的数据分成称为分区的许多小的子集.倘若硬盘丢失了分区表,数据就无法按顺序读取和写入,导致无法操作. 2. 表分区分类 (1)范围分区 create table tabl ...
- 【爬坑】远程连接 MySQL 失败
问题描述 远程连接 MySQL 服务器失败 报以下错误 host 192.168.23.1 is not allowed to connect to mysql server 解决方案 在服务器端打开 ...
- 前端数据库——WebSQL和IndexedDB
一.WebSQL WebSQL是前端的一个独立模块,是web存储方式的一种,我们调试的时候会经常看到,只是一般很少使用.并且,当前只有谷歌支持,ie和火狐均不支持. 我们对数据库的一般概念是后端才会跟 ...
- 5.06-re
import re # 贪婪模式 从开头匹配到结尾 默认 # 非贪婪 one = 'mdfsdsfffdsn12345656n' two = "a\d" pattern = re. ...