LeetCode 394. Decode String

本文介绍了解码字符串问题的解决方法,通过使用两个栈来处理数字和字符串,实现字符串的正确解码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

【题目大意】

  结合实例此题不难理解,即按照字符串中整型与字符型的要求,将字符串转换成特定的另一字符串

【 思路】

     1.分两个栈numStack与strStack 分别记录 整型(倍数)与字符串(目标量);     2.由于输入是String类型,但我们要拿到两种属性,故将其转换为字符数组,然后开始遍历次字符数组;     3.对于numStack       1)压栈:           由于“重复的次数”可以为两位数及以上,故我们不能将独到的整型字符直接压栈,此处用到乘10累加法获得两位数以上的整型           仅当读到'['字符时,才可表明此时的“重复的次数”信息记录完成,方可压栈;       2)出栈:           当strStack出栈结束时,numStack方可出栈;     4.对于strStack      1)压栈:         每当读到的字符不为']'(且不为整型)时方可压栈;      2)出栈:         独到字符']'时代表这一中括号中的字符要接受遍历,故将最近的'['之前的字符全部出栈;     5.对于出栈后的处理        两栈均出栈后将采用循环来得到这一阶段的最终输出,而后将这一阶段的最终输出继续压紧strStack之中,因为其可能有嵌套关系存 如“3[a2[c]]”;     6.在字符数组遍历结束后        此时strStack中装载的就是所有“阶段”的最终结果的逆序,只需将strStack出栈后再将其结果逆序便得到最终答案;        【代码】  

   public static String decodeString(String s) {
    	StringBuffer result = new StringBuffer();//记录最终结果
    	Stack<Integer> numStack = new Stack();//专门记录整形数字的栈
    	Stack<String> strStack = new Stack();//专门记录字符串型的栈
    	//先转换成字符数组
    	char c[] = s.toCharArray();
    	int len = c.length;
    	int num = 0;
    	for(int i=0;i<len;i++){
    		if(Character.isDigit(c[i])){
    			num = num*10 + c[i]-'0';//有可能前面的倍数是两位数以上故要 乘10后再累加,且num不能马上入栈
    		}else{
    			//每当读到'['时,则代表之前计算的是num的最后一位,num可以入栈
    			if(c[i] == '[')	
        			numStack.push(num);
    			//每当读到']'时,则代表距离最近的'['之后的字符元素可以出栈接受Num栈中出栈的num的循环
    			if(c[i] == ']'){
    				StringBuffer cur = new StringBuffer(); //记录当前的出字符栈结果
    				StringBuffer tmpRes = new StringBuffer(); //记录按前面整型循环后的出字符栈结果
    				String temp = strStack.pop();
    				//当出栈到'['时 终止循环
    				while(!temp.equals("[") && !strStack.isEmpty()){
    					cur.append(temp);
    					temp = strStack.pop();
    				}
    				//sb.reverse();
    				//num栈出栈一个遍历数字
    				int count = numStack.pop();
    				//拿到当前循环后的结果,并且再将此结果压入str栈,此时tmpRes也是逆序的,只需最后将最终结果逆序则可
    				for(int j=0;j<count;j++){
    					tmpRes.append(cur.toString());
    				}
    				strStack.push(tmpRes.toString());
    			}else{
    				//若为读到']' 则表示可以继续压栈
    				strStack.push(c[i]+"");
    			}
    			//num归0,以便下次的计数
       			num = 0;
    		}
    	}
    	//遍历完整个字符数组后,此时strStack中装的既是所求结果
    	while(!strStack.isEmpty()){
    		result.append(strStack.pop());
    	}
    	result.reverse();
    	return result.toString();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值