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

给定一个经过编码的字符串,返回它解码后的字符串。

编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。

你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。

此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。

示例:

s = "3[a]2[bc]", 返回 "aaabcbc".
s = "3[a2[c]]", 返回 "accaccacc".
s = "2[abc]3[cd]ef", 返回 "abcabccdcdcdef".

题解:此题首先需要用栈来保存记录该string中所有的元素,但是碰到[ ],就需要计算该中括号里面的字符串了,同时,会有两个大坑需要注意!在代码中会展现。

public static String decodeString(String s)
    {
        int length = s.length();
        Stack<String> stack = new Stack<>();
        for(int i = 0; i < length; i++)
        {
            //System.out.print(s.charAt(i) + " ");
            if(s.charAt(i) == ']')
            {
                String str = "";
                while(!stack.peek().equals("["))
                {
                    if(stack.peek().length() > 1)
                    {
                        StringBuffer sb = new StringBuffer(stack.pop());   //这里是最需要注意的地方,就是如果存储在stack中的某个string元素的长度超过1,那么不能直接把它加到新的string中,而是要把原来在stack中的这个元素反转,然后再存储到str中,用于进行下一次的运算
                        String ss = sb.reverse().toString();
                        str += ss;
                    }
                    else
                     str += stack.pop();
                }
                System.out.println(str);
                StringBuffer sb = new StringBuffer(str);
                str = sb.reverse().toString();
                System.out.println(stack.peek());
                stack.pop();
                int num = 0;
                int k = 0;
                //System.out.println(stack.peek().charAt(0));
                while(!stack.isEmpty() && Character.isDigit(stack.peek().charAt(0)))       //这里也是一个大坑,因为可能会碰到10[leetcode]的形式,但是因为初始的变量为string类型,那么在遍历的时候,碰到10,应该要把这两个char都重新利用10的幂乘运算来计算
                {
                    num += Integer.parseInt(stack.pop()) * Math.pow(10,k);     //而且得判断该stack是否为空,如果已经为空,那么就直接跳出此循环
                    //System.out.println(num);
                    k++;
                }
                //int num = Integer.parseInt(stack.pop());
                System.out.println(str);
                System.out.println(num);
                String newstring = str;
                for(int j = 1; j <= (num - 1); j++)
                    str += newstring;
                System.out.println(str);
                System.out.println(stack.isEmpty());
                stack.push(str);
                System.out.println(stack.isEmpty());
            }
            else
                stack.push(String.valueOf(s.charAt(i)));
        }
        String newstr = "";
        Stack<String> newstack = new Stack<String>();
        while(!stack.isEmpty())
        {
            newstack.push(stack.pop());
        }
        while(!newstack.isEmpty())
            newstr += newstack.pop();
        return newstr;
    }

这道题关键就在于思考问题的仔细程度,思维的完善性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值