解压字符串

题目:
某位程序员想出了一种压缩字符串的方法,压缩后的字符串如下: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());
    }
}
posted @ 2017-08-27 09:54  傍晚的羔羊  阅读(572)  评论(0编辑  收藏  举报