将一段压缩后的字符串解压缩,并且排序输出。例如:a3b2,输出结果为bbaaa

import java.util.Scanner;

/**
 * @Description
 * 题目描述
 * 将一段压缩后的字符串解压缩,并且排序输出。
 * 解压规则:每个字符串后边跟一个数字,表示这个字符串的重复数字将每个字符串的字符重复次数升序排序,并输出结果。
 * 例如:a3b2,输出结果为bbaaa
 * a11b2bac3bad3abcd2
 * 思路:
 * 将每个字符串和数字分别取出存到两个数组
 * 对这两个数组进行排序
 * 最后进行重复的拼接工作。
 * @Author 19040838
 * @Date 2021.02.02 15:26
 **/
public class ExtractZip {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String input = sc.nextLine();
            System.out.println(extractZip(input));
        }
    }

    private static String extractZip(String input) {
        if (input == null || "".equals(input.trim())) {
            return "";
        }
        char[] chars = input.toCharArray();
        StringBuilder temp = new StringBuilder();
        String[] strArr = new String[input.length() / 2];
        int[] numArr = new int[input.length() / 2];
        int k=0;
        int count = 0;
        for(int i = 0, n = chars.length; i < n; i++) {
            if((chars[i] + "").matches("[a-z]")){
                temp.append(chars[i]);
                continue;
            } else{
                strArr[k] = temp.toString();
            }
            if((chars[i] + "").matches("[0-9]")) {
                count = count * 10 + Integer.parseInt(chars[i] + "");
                if(i != chars.length - 1 && (chars[i + 1] + "").matches("[0-9]")) {
                    continue;
                } else{
                    numArr[k] = count;
                }
            }
            k += 1;
            count = 0;
            temp = new StringBuilder();
        }
        for(int i = 0;i < numArr.length - 1;i++){
            for(int j = 0;j < numArr.length - 1 - i;j++){
                if(numArr[j] > numArr[j + 1]){
                    int temp1 = numArr[j];
                    numArr[j] = numArr[j + 1];
                    numArr[j + 1] = temp1;
                    String tempStr = strArr[j];
                    strArr[j] = strArr[j + 1];
                    strArr[j + 1] = tempStr;
                }
            }
        }
        StringBuilder result = new StringBuilder();
        for(int j = 0; j < strArr.length; j++) {
            for(int i = 0;i < numArr[j];i++) {
                result.append(strArr[j]);
            }
        }
        return result.toString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软软的铲屎官

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值