解压字符串

题目:
某位程序员想出了一种压缩字符串的方法,压缩后的字符串如下:3{a}2{bc}, 3{a2{c}},2{abc}3{cd}ef,现在需要你写出一个解压程序,还原原始的字符串,如:
s=“3{a}2{bc}”return “aaabcbc” , s=”3{a2{c}}” return “accaccacc” ,s=”2{abc}3{cd}ef” return “abcabccdcdcdef”,重复次数可以确保为一个正整数。

解答:
递归方法:

import java.util.Scanner;

public class Main {
    public static String unZip(String s) {
        if(s == null || s.length() ==0) {
            return s;
        }

        StringBuilder sb = new StringBuilder();
        int sum = 0;
        int i = 0;

        while(i<s.length()) {
            if(s.charAt(i) > '0' && s.charAt(i) <= '9') {
                sum = sum*10 + (s.charAt(i)-'0');
                i++;
            } else if(s.charAt(i)=='{') {
                int j = i+1;
                int times = 1;

                while(j++<s.length()) {
                    if(s.charAt(j)=='{') {
                        times++;
                    } else if(s.charAt(j)=='}') {
                        times--;    
                    }
                    if(times ==0) {
                        break;
                    }
                }

                String sub = s.substring(i+1, j);

                String ss = unZip(sub);

                for(int k=0; k<sum; k++) {
                    sb.append(ss);
                }
                //下一个"{"开始
                sum = 0;
                i=j+1;

            } else {
                sb.append(s.charAt(i));
                i++;
            }
        }

        return sb.toString();   
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inValue = in.nextLine();

        String res = unZip(inValue);
        System.out.println(res);
    }   
}

非递归方法:

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inValue = in.nextLine();

        if(inValue == null || inValue.length() ==0) {
            return;
        }

        char[] c = inValue.toCharArray();

        Stack<Integer> times = new Stack<>();
        Stack<String> ch = new Stack<>();

        StringBuilder sb = new StringBuilder();
        StringBuilder shuzi = new StringBuilder();
        StringBuilder zfc = new StringBuilder();

        for(int i=0; i<c.length; i++) {
            int res = c[i] - 48;
            if(res<1 || res>9 && res !=75 && res !=77) {
                if(times.isEmpty()) { 
                    sb.append(c[i]);
                } else {                
                    zfc.append(c[i]);
                }
            } else if(res>=1 && res<=9){ 
                shuzi.append(c[i]);
            } else if(res == 75) {
                times.push(Integer.parseInt(shuzi.toString()));
                shuzi = new StringBuilder();
                if(zfc.length() !=0) {
                    ch.push(zfc.toString());
                    zfc = new StringBuilder();
                }
            } else {                
                if(zfc.length() !=0) {
                    ch.push(zfc.toString());
                    zfc = new StringBuilder();
                }
                StringBuilder temp = new StringBuilder();
                while(!times.isEmpty() && !ch.isEmpty()) {
                    int t = times.pop();
                    String s = ch.pop();
                    StringBuilder temp1 = new StringBuilder();
                    temp1.append(s);
                    temp1.append(temp.toString());
                    StringBuilder temp2 = new StringBuilder();
                    for(int k=0; k<t; k++) {
                        temp2.append(temp1.toString());
                    }
                    temp = temp2;
                }
                sb.append(temp);
            }
        }

        System.out.println(sb.toString());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值