Leetcode Decode String
来源:互联网 发布:怎么去兼职做美工外包 编辑:程序博客网 时间:2024/05/29 07:56
题意:将字符串展开,数字表示重复的次数,方括号中表示重复的内容。
思路:DFS,因为可能有嵌套,所以将方括号中的内容进行搜索。
class Solution {public: string decodeString(string s) { return dfs(s); } string dfs(string s) { if(s.length() == 0) return s; string re; string num; for(int i = 0; i < s.length(); ++ i) { if(s[i] >= '0' && s[i] <= '9') { num += s[i]; continue; } if(s[i] == '[') { int mark = 1; string subs; while( ++ i < s.length()) { if(s[i] == ']' && mark == 1) { break; } if(s[i] == '[') { mark ++; } if(s[i] == ']') { mark --; } subs += s[i]; } string tempre = dfs(subs); //cout << tempre << " " << subs << endl; //cout << num << endl; int count = std::stoi(num); num =""; //cout << count << endl; for(int j = 0; j < count; ++ j) re += tempre; continue; } re += s[i]; } return re; }};
0 0
- 【Leetcode】394. Decode String
- [Leetcode] Decode String
- 【leetcode】394. Decode String
- LeetCode: Decode String
- leetcode ----394. Decode String
- LeetCode[394] Decode String
- [leetcode] 394. Decode String
- [Leetcode]394. Decode String
- Leetcode Decode String
- 【LeetCode】 394. Decode String
- LeetCode 394. Decode String
- Leetcode 394. Decode String
- [leetcode]394. Decode String
- LeetCode 394. Decode String
- leetcode-394-Decode String
- Leetcode-394. Decode String
- LeetCode 394. Decode String
- Leetcode 394. Decode String
- 学习运维——用户、用户组、用户权利下放
- 【JZOJ 4939】平均值
- 涉及JSP页面请求、响应、切换和数据传递的5个内置对象
- 放下 2017
- 莫队(三)核心代码实现
- Leetcode Decode String
- 学习运维——文件权限、控制对文件访问
- poj 2891 Strange Way to Express Integers (中国剩余定理不互质)
- Angular2 中的依赖注入
- 《安卓第一行代码》笔记
- vc++中HBRUSH的几种用法
- 用树莓派做控制板,mysql 数据库出现超时问题的解决
- 《计算机网络》
- onvif协议简述