华为笔试题

题目描述

将一段压缩后的字符串解压缩,并且排序输出。

解压规则:每个字符串后边跟一个数字,表示这个字符串的重复数字

                  将每个字符串的字符重复次数升序排序,并输出结果。

例如:a3b2,输出结果为bbaaa

思路:

将每个字符串和数字分别取出存到两个数组

对这两个数组进行排序

最后进行重复的拼接工作。

程序:

public class Dzip {

    public static void main(String[] args) {
        String str1 = "b10c13abc3";
        System.out.println(decompressChar(str1));
    }

    private static String decompressChar(String str) {
        StringBuilder sb = new StringBuilder();
        char[] chars = str.toCharArray();
        int count = 0;
        StringBuilder temp = new StringBuilder();
        String aaa[]=new String[str.length()/2];
        int num[]=new int[str.length()/2];
        int k=0;
        for(int i = 0, n = chars.length; i < n; i++) {
            if((chars[i] + "").matches("[a-z]")){
                temp.append(chars[i]);
                continue;
                //
            }
            else{aaa[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{num[k]=count;}

            }

           k=k+1;
            count=0;
            temp=new StringBuilder();


        }
        for(int i=0;i<num.length-1;i++){
            for(int j=0;j<num.length-1-i;j++){
                if(num[j]>num[j+1]){
                    int temp1=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp1;
                    String tempstr=aaa[j];
                    aaa[j]=aaa[j+1];
                    aaa[j+1]=tempstr;
                }
            }
        }

        for(int j = 0; j < aaa.length; j++) {
            for(int i=0;i<num[j];i++)
                sb.append(aaa[j]);
        }
        return sb.toString();

    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值