编程练习(第七周)
2017-04-15 15:08
162 查看
394. Decode String
Add to ListDescriptionHintsSubmissionsSolutions
Total Accepted: 26024
Total Submissions: 63724
Difficulty: Medium
Contributor: LeetCode
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
3aor
2[4].
题解: string decodeString(string s) {
stack<string> chars;
stack<int> nums;
string res;
int num = 0;
for(char c : s) {
if(isdigit(c)) {
num = num*10 + (c-'0');
}
else if(isalpha(c)) {
res.push_back(c);
}
else if(c == '[') {
chars.push(res);
nums.push(num);
res = "";
num = 0;
}
else if(c == ']') {
string tmp = res;
for(int i = 0; i < nums.top()-1; ++i) {
res += tmp;
}
res = chars.top() + res;
chars.pop(); nums.pop();
}
}
return res;
}
相关文章推荐
- JAVA--第七周实验-- 继续上周封装对象的编程练习,完成矩阵的运算。
- 《OpenCV3.0 编程入门》书中3.1.9节图像的载入、显示与输出示例程序练习报异常问题
- C++ Primer Plus 第6版 中文版 第5章编程练习
- Java之-----------TCP编程(小练习大用处)
- C语言编程(练习5:字符串与字符串函数)
- java界面编程 练习 : 记事本
- c++ Primer Plus(第六版)第九章编程练习之路
- unix环境编程练习 (1)
- 编程小练习
- python核心编程十四章练习
- 编程基本功训练:流程图画法及练习
- 慕课网-安卓工程师初养成-5-3 编程练习优化
- C++ Primer Plus第六版编程练习11.7解答
- 学习c编程练习1
- C++primer plus第六版课后编程练习答案4.1
- C++primer plus第六版课后编程练习答案5.4
- PYTHON黑帽编程1.5 使用WIRESHARK练习网络协议分析
- C++primer plus第六版课后编程练习答案6.8
- 黑马程序员_网络编程二(Tcp练习,URL-URLConnection,网络知识,域名解析)
- 第十一章编程练习(3)