[LeetCode] Decode String 题解
题目
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
大概意思是根据指定的格式将字符串进行展开
思路
- 递归的思路
递归计算出括号最里面的字符串,依次再处理外面一层的字符串,每个单元内的字符串类似于一个结点,个数则为结点的个数。父结点则是将这些个数的字符串组合在一起,以此类推到跟结点,就是我们要求的结果。
- 迭代的思路
用两个栈来分别保存下单元中的个数,另一个则保存单元中的字符串,注意的是,要将最新的处理完后的字符串加入到栈中。一直加入,直到最后返回栈顶字符串则为所求结果。
实现
//
//
#include "../PreLoad.h"
/*
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
*/
class Solution {
public:
// 错误做法
string decodeString(string s) {
if (s == "") {
return "";
}
string result = "";
stack<pair<char, int>> codes;
codes.push({s[0], 0});
while (!codes.empty()) {
auto content = codes.top();
codes.pop();
// 表明开始是数字
if (content.first >= '0' && content.first <= '9') {
int start = content.second;
string nums = "";
nums.push_back(content.first);
while (s[start] != '\n' && s[start] != '[') {
content.first += s[start];
start++;
}
// 计算单位的字符串
int num = atoi(nums.c_str());
string str = "";
for (int i = 0; i < num; i++) {
str += content.first;
}
result += str;
// 找到下一个单位起始的位置
while (s[start] != '\n' && s[start] != ']') {
start++;
}
if (start != s.size()-1) {
start++;
codes.push({s[start], start});
}
}
}
return result;
}
/**
* 迭代DFS的做法
*
* @param s
* @return
*/
string decodeString2(string s) {
int cnt = 0; //每个单元里的数字个数
string content = "";
int len = s.size();
int i = 0;
stack<string> strs;
stack<int> cnts;
while (i < len) {
// 为数字的情况
if (i < len && s[i] >= '0' && s[i] <= '9') {
cnt = cnt * 10 + (s[i] - '0');
}
// 为左括号的情况
else if (i < len && s[i] == '[') {
cnts.push(cnt);
strs.push(content); //因为存在内嵌的情况,所以保存下之前的单元内的字符串
cnt = 0;
content.clear(); //清空用来保存后面单元的字符串
}
// 为右括号的情况
else if (i < len && s[i] == ']') {
int cnt_temp = cnts.top();
cnts.pop();
while (cnt_temp--) {
strs.top() += content;
}
// 将之前的字符串进行累加以后得到新的单元的内容
content = strs.top();
strs.pop();
}
// 中间的字符串
else {
content += s[i];
}
i++;
}
// 可能存在没有]的单元
return strs.empty() ? content : strs.top();
}
/**
* 递归DFS
* 计算单元格内字符串的内容,再根据个数进行累加
* 注意字符串下标和边界条件
*
* @param s
* @return
*/
string decodeString3(string s) {
int len = s.size();
if (len == 0) {
return "";
}
int i = 0;
return decodeHelper(s, i);
}
string decodeHelper(string s, int& i) {
string result = "";
int len = s.size();
while (i < len && s[i] != ']') {
// 非数字而是字符的情况下
if (s[i] < '0' || s[i] > '9') {
result += s[i++];
}
else {
// 计算数字
int cnt = 0;
while (s[i] >= '0' && s[i] <= '9' && i < len) {
cnt = cnt * 10 + (s[i++] - '0');
}
// 移动到字符串开始的位置
i++;
// 获得单元内的字符串,此时递归返回i的位置是]的位置
string str = decodeHelper(s, i);
//移动到下一个单元的位置
i++;
while (cnt--) {
result += str;
}
}
}
return result;
}
void test() {
string str = "3[a2[c]]";
cout << "result : " << decodeString3(str) << endl;
}
};
总结:这些和在树中使用DFS的思想是类似的。
[LeetCode] Decode String 题解的更多相关文章
- [LeetCode] Decode String 解码字符串
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- LeetCode——Decode String
Question Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_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) 44
394. 字符串解码 394. Decode String 题目描述 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 enco ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- LeetCode——Reverse String
LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- Leetcode -- 394. Decode String
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
随机推荐
- C++ 头文件系列 (algorithm)
简介 algorithm头文件是C++的标准算法库,它主要应用在容器上. 因为所有的算法都是通过迭代器进行操作的,所以算法的运算实际上是和具体的数据结构相分离的 ,也就是说,具有低耦合性. 因此,任何 ...
- 【福利大放送】不止是Android,Github超高影响力开源大放送,学习开发必备教科书
一.写在前面 最近项目重构,时间贼多,也没什么时间更新博客,个人的开源项目也是多时没有更新了:github地址,然而没有更新不代表我不在乎,后面一有空还是会继续提交的. 还是来冒个泡,给大家献上一些福 ...
- Ubuntu16.04+Theano环境
安装Anaconda: 官网下载Anaconda 切换到下载目录 cd ~/下载/ 用bash运行下载好的.sh文件 bash Anaconda2--Linux-x86_64.sh 进入欢迎界面 We ...
- 远程推送-----iOS
前言 说一下我了解的推送 正文 APNs--------Apple Push Notification service 1 远程推送的大概流程及其原理 我们的设备联网时(无论是蜂窝联网还是Wi-Fi联 ...
- python中的select模块
介绍: Python中的select模块专注于I/O多路复用,提供了select poll epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...
- Boost.Hana在visual studio 2017 rc中的残缺使用
最新的visual studio还不支持hana,不知道vs2017正式版本出后会不会支持.等不及了,先用rc版试试吧. 1.从https://github.com/boostorg/hana下载或拉 ...
- 深入浅出妙用 Javascript 中 apply、call、bind
这篇文章实在是很难下笔,因为网上相关文章不胜枚举. 巧合的是前些天看到阮老师的一篇文章的一句话: "对我来说,博客首先是一种知识管理工具,其次才是传播工具.我的技术文章,主要用来整理我还不懂 ...
- 在vim中,使用可视化拷贝(剪切)粘贴文本
1 定位光标到你想要开始剪切的位置 2 按v选择字符(按V是选择整行) 3 移动光标到你想要结束剪切的位置 4 按d是为了剪切(按y是为了拷贝) 5 移动光标到你想要粘贴的位置 6 按P是在光标之前 ...
- C#丨爬虫基础
在前几天看到一片公众号的文章是关于.NET玩爬虫. 所以今天小编索性来try一下,恰好小编最近在关注房价这一块的,索性就写了一个例子抓取房产信息的. 不善言辞的小编直接给出代码吧!相信读者也等不及了. ...
- 《汇编语言程序设计》——仿windows计算器
<汇编语言程序设计> ——计算器程序设计 目录 一. 题目与目标 1. 题目 2. 学习目的 二. 分析与设计 1. 系统分析 2. ...