LeetCode——Decode String
Question
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".
Solution
用两个栈实现。
Code
class Solution {
public:
string decodeString(string s) {
string res = "";
for (int i = 0; i < s.length(); i++) {
if ((s[i] >= 'a' && s[i] <= 'z') || s[i] == '[') {
// 10[leetcode]ef, 处理ef
if (nums.empty()) {
res += s[i];
} else
letters.push(s[i]);
}
if (s[i] >= '0' && s[i] <= '9') {
string tmp = "";
tmp += s[i];
// 处理多个连续的数字
for (int j = i + 1; j < s.length(); j++) {
if (s[j] >= '0' && s[j] <= '9') {
tmp += s[j];
i++;
}
else
break;
}
stringstream ss;
ss << tmp;
int value;
ss >> value;
nums.push(value);
}
if (s[i] == ']') {
int value = nums.top();
nums.pop();
string tmp = "";
while (1) {
char c = letters.top();
if (c != '[')
tmp += c;
letters.pop();
if (c == '[')
break;
}
reverse(tmp.begin(), tmp.end());
string tmp2 = "";
for (int i = 0; i < value; i++) {
tmp2 += tmp;
}
// 如果为空了,就直接累加到结果里,否则压入栈中
if (nums.empty()) {
res += tmp2;
} else {
for (int i = 0; i < tmp2.length(); i++)
letters.push(tmp2[i]);
}
}
}
return res;
}
stack<int> nums;
stack<char> letters;
};
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 题解
题目 题目 s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return " ...
- [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 ...
随机推荐
- 小程序 Page is not constructed because it is not found.
如下错误一般发生在点击事件切换页面的时候 解决方式: 在需要切换到的那个页面的js文件中添加Page({ })方法即可解决此问题. Tis:在js文件中输入Page回车,可自动添加Page方法,包括里 ...
- VMwareWorkstation与Device/CredentialGuard不兼容
win10的虚拟与VMware Workstation的虚拟有冲突,需要关闭win10自带的虚拟Hyper-V功能. 1.Windows键 --- 设置 --- 搜索 “控制面板” --- 程序 - ...
- Macbook pro 13" compile Apollo 2.5
STEPS: 0. Install Homebrew 1. Install 'Docker for Mac 18.03+',配置CPUs (n个CPUs,Bazel开n个线程编译), Memory ...
- flask系列
1.flask基础 2.flask上下文 3.flask源码剖析--请求流程 4.数据库连接池DButils 5.Flask-Session 6.WTForms 7.Flask-SQLAlchemy ...
- window 如何枚举设备并禁用该设备和启用该设备?如何注册设备热拔插消息通知?
目前实现的功能: 1.设备枚举 2.设置设备禁用和启用 3.注册设备热拔插消息通知 4.获取设备 vid pid 数值 需要链接的库 SetupAPI.lib DeviceManager 类如下: D ...
- alexnet,VGG,googlenet,resnet
非常好的一篇:https://my.oschina.net/u/876354/blog/1637819 alexnet和VGG没什么特别的,VGG深一些. Deep learning 实际上是一种 f ...
- Python性能优化的20条建议
优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(1).不同的场 ...
- windows 最大支持线程数
WINDOWS操作系统中可以允许最大的线程数 默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来 ...
- (2.10)备份与还原--利用T-SQL进行备份还原
常用: /************ 一.数据库备份 ************/ with init,format,stats=1init:覆盖format:格式化备份集stats=1:显示进度条 ST ...
- 过滤adb logcat 日志
原文地址http://blog.csdn.net/listening_music/article/details/7518990 另外比较好的文章http://blog.csdn.net/liao27 ...