394. Decode String

来源:互联网 发布:北美海关数据 编辑:程序博客网 时间:2024/06/03 13:54

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".

class Solution {public:    string decodeString(string s) {       stack<char> t;vector<int> nums;int count=0;string tmp="",result="";while(count<s.length()){if(s[count]==']'){while(t.top()!='['){tmp.push_back(t.top());t.pop();}t.pop();while(!t.empty()&&isdigit(t.top())){nums.push_back(t.top()-'0');t.pop();}int num=0;            reverse(nums.begin(),nums.end());for (int i=0;i<nums.size();i++){num=num*10+nums[i];}reverse(tmp.begin(),tmp.end());for (int i = 0; i < num; ++i){result+=tmp;}for (int i = 0; i < result.length(); ++i){t.push(result[i]);}result="";tmp="";count++;nums.clear();continue;}t.push(s[count]);count++;}result="";while(!t.empty()){result.push_back(t.top());t.pop();}reverse(result.begin(),result.end());return result;    }};

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 产后发汗有科学依据吗 月子发汗怎么发 上门发满月汗详细步骤 发汗后注意事项 满月发汗后注意事项 出月子发汗怎么发 感冒发汗 满月发汗多少钱 如何发汗 吃什么发汗 月子怎么发汗 发汗的中药 产妇发汗 产后满月发汗 产后发汗的好处 产后发汗哪种好 产后发汗好吗 产后发汗有什么好处 满月发汗的好处和坏处 产后发汗发多长时间 月子发汗多少钱一次 满月发汗注意事项 发汗汤 孕妇发汗 满月发汗好不好 满月发汗价格 月子发汗多少钱 发汗时间 发发汗 发汗中药 满月发汗怎么发 发汗服 坐月子怎么发汗 中药发汗 坐月子发汗怎么发 发汗解表 满月发汗有用吗 月子里可以发汗吗 坐月子如何发汗 满月发汗需要多长时间 全身发汗是怎么回事