华为OD机考真题-解压报文

/**
* 解压报文,输入一个压缩后的报文,返回解压后的原始报文
* 压缩规则 n[str] 括号内表示压缩的内容 n表示重复次数
* 如:3[m2[c]] 输出mccmccmcc
* 10[k]2[mn3[j2[op]]]
*/

  public static void main(String[] args) {
        Stack number = new Stack<>();
        Stack str = new Stack<>();
        String value = "3[m2[c]]";
        int index1 = 0;
        StringBuffer sb = new StringBuffer();
        StringBuffer sbTemp1 = new StringBuffer();
        StringBuffer sbTemp2 = new StringBuffer();

        StringBuffer sbLoop = new StringBuffer();
        //方法1
        for (int i = 0 ;i < value.length(); i++){
            //入栈
            //方法1.通过正则表达式跟截取值分别入栈
            if (value.charAt(i) == '[' || value.charAt(i) == ']'){
                String temp = value.substring(index1,i);
                Pattern pattern = Pattern.compile("\\d+");
                Matcher matcher = pattern.matcher(temp);
                while (matcher.find()){
                    number.push(Integer.valueOf(matcher.group()));
                }

                Pattern pattern1 = Pattern.compile("[a-zA-Z]+");
                Matcher matcher1 = pattern1.matcher(temp);
                while (matcher1.find()){
                    str.push(String.valueOf(matcher1.group()));
                }
                index1 = i;
            }

            //出栈 "10[k]2[mn3[j2[op]]]"
            if (value.charAt(i) == ']') {
                //从数字栈跟字符串栈中推出值,进行拼接报文
                while (!str.empty() && !number.empty()) {
                    String temp = str.pop();
                    int size = number.pop();
                    sbTemp2.insert(0,temp);
                    for (int k = 0; k < size; k++) {
                       sbTemp1.append(sbTemp2);
                    }
                    sbTemp2 = sbTemp1;
                    sbTemp1 = new StringBuffer();
                }
            }
            sb = sb.append(sbTemp2);
            sbTemp1 = new StringBuffer();
            sbTemp2 = new StringBuffer();
        }
 System.out.println("sb:" + sb);
    }
}

你可能感兴趣的:(华为OD最新机试真题训练题,算法,华为od,java)