LeetCode:394. Decode String

https://leetcode.com/problems/decode-string/description/

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

如果對數據結構隊列和棧,波蘭式、逆波蘭式,這道題就不難了。當然,也可以用遞歸寫。
思路:1、如果遇到的是數字,就放入棧中,這里用‘-’字符代表一個數,因為數字可能大于9,把具體的數字保存在
另外一個棧中了。
2、當遇到是右括號的時候,把棧頂所有字符元素str出棧,還有數字N也出棧,然后N倍的str 又壓入棧中。
3、最后棧中的元素就是結果了。注意下順序。

class Solution 
{
    public:
        string decodeString(string s)
        {
            string retstr;          
            if(0==s.length())
                return retstr;
            string sub_str;
            stack<char> all;
            stack<int>  stack_cnt;
            int curPos = 0;
            char curCh = 0;
            int cnt = 0;
            while( curPos<s.length())
            {
                curCh = s[curPos];
                if( isalpha(curCh) )
                {
                    all.push(curCh);
                }
                else if( isdigit(curCh))
                {
                    sub_str = s.substr(curPos);
                    cnt = atoi(sub_str.c_str());
                    all.push('-');
                    stack_cnt.push(cnt);
                    while(curPos<s.length() && s[curPos]!='[')
                        ++curPos;
                }
                else if(curCh==']')
                {
                    string str;
                    int cnt = 0;
                    char ch = 0;
                    while(!all.empty())
                    {
                        ch = all.top();
                        all.pop();
                        if(ch=='-')
                            break;
                        str.push_back(ch);
                    }               
                    cnt = stack_cnt.top();
                    stack_cnt.pop();
                    for(int i=0; i<cnt; ++i)
                    {
                        for(int j=str.length()-1; j>=0; --j)
                            all.push(str[j]);
                    }
                }
                ++curPos;
            }

            stack<char> tmpstack;
            while(!all.empty())
            {
                curCh = all.top();
                all.pop();
                tmpstack.push(curCh);
            }
            while(!tmpstack.empty())
            {
                curCh = tmpstack.top();
                tmpstack.pop();
                retstr.push_back(curCh);
            }
            return retstr; 
        }
};
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容