394. Decode String
来源:互联网 发布:崛起网络f18团平台图片 编辑:程序博客网 时间:2024/06/05 11:20
class Solution {
public:
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;
}
};
public:
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;
}
};
阅读全文
0 0
- 【Leetcode】394. Decode String
- 394. Decode String
- 394. Decode String
- 394. Decode String
- 【leetcode】394. Decode String
- 394. Decode String
- leetcode ----394. Decode String
- 394. Decode String
- 394. Decode String
- 394. Decode String
- 394. Decode String
- [leetcode] 394. Decode String
- [Leetcode]394. Decode String
- 394. Decode String
- 【LeetCode】 394. Decode String
- 394. Decode String
- LeetCode 394. Decode String
- Leetcode 394. Decode String
- 实名函数、匿名函数;
- 自定义布局的对话框
- H.264 JM的学习笔记
- android自动化测试工具【UiAutomator】——UiWatcher(一)
- hbase简单分析及其命令
- 394. Decode String
- 1047. 编程团体赛(20)
- JDk目录介绍
- Linux下的进程概论与编程三(进程间通信的5种方式)
- 【LeetCode】115.Distinct Subsequences
- 通熟易懂学习卷积神经网络(CNN)
- Derek的成长之路——前端与移动开发之HTML(基础篇)
- 封装好的webview
- 营创学院 肖知兴 分享课