LeetCode: Decode String

题目:
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”.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”.

解法一:
利用栈来实现,‘[’控制入栈,‘]’控制出栈。用一个栈来保存重复的次数,一个栈来保存重复的内容。每次遇到‘]’时,将中括号内的所有字符出栈,重复相应的次数后重新入栈,可以以‘[’的入栈来标记括号内字符的范围。当所有字符遍历完后,栈中保存的即是结果,将其弹出即可。时间复杂度不超过O(len^3),len表示字符串的长度。Accepted的代码:

class Solution {
public:
    string decodeString(string s) {
        string result="";
        stack<int> times;
        stack<char> word;
        string temp="";

        int count=0;//记录当前重复的次数
        for(int i=0;i<s.length();i++)
        {
            if(s[i]>='0'&&s[i]<='9') count=count*10+s[i]-'0';
            else if(s[i]=='[')
            {
                times.push(count);
                count=0;
                word.push('[');
            }
            //将括号内的字符重复相应次数后按次序重新入栈
            else if(s[i]==']')
            {
                int n=times.top();
                times.pop();
                while(word.top()!='[')
                {
                    temp+=word.top();
                    word.pop();
                }
                word.pop();
                for(int j=0;j<n;j++)
                    for(int t=temp.length()-1;t>=0;t--)
                        word.push(temp[t]);
                temp="";
            }
            else word.push(s[i]);
        }
        //清空栈
        while(!word.empty())
        {
            temp+=word.top();
            word.pop();
        }
        for(int i=temp.length()-1;i>=0;i--) result+=temp[i];
        return result;
    }
};

解法二:
深度优先搜索:把一个括号中的所有内容看做一个整体,一次递归函数返回一对中括号中解码后的字符串。在dfs函数中,对字符串中的字符遍历,如果是数字,循环读入所有数字并转换,转换完后下一个字符一定是‘[’,在遍历中用下标跳过该字符;如果是字母,则直接存入结果中。递归的结束条件为字符串遍历结束或遇到右括号。Accepted的代码:

class Solution {
public:
    string decodeString(string s) {
        int i=0;
        return dfs(s,i);
    }
    //递归函数
    string dfs(string s, int& i)
    {
        string result = "";


        while (i<s.size()&&s[i]!=']')//循环终止条件的否命题
        {
            if (s[i]<'0'||s[i]>'9')  result+=s[i++];
            else 
            {
                int count=0;//当前重复次数
                while (i<s.size()&&s[i]>='0'&&s[i]<='9') count=count*10+s[i++]-'0';

                i++;//跳过左括号
                string t=dfs(s,i);
                i++;//跳过右括号
                while(count-->0) result+=t;
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值