棒!添加注解版!    public static String parse(String str){         StringBuilder sb = new StringBuilder();         LinkedList<Integer> len = new LinkedList<>();         String temp = ""; // 记录括号内的字符串         // 针对字符串,处理顺序依次为:去括号,统计出现次数,拼接结果         for(int i=0;i<str.length();i++){             char c = str.charAt(i);             if(c == '('){                 len.add(sb.length());                 continue;             }             if(c == ')'){                 // 移除上一个括号开始时的下标                 int newLeftIndex = len.remove(len.size()-1);                 temp = sb.substring(newLeftIndex);                 i++;             }             // 处理数字             int start = i;             while (i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9'){                 i++;             }             // 获取字符后面的数字             if(i-start>=1){                 int count = Integer.valueOf(str.substring(start,i));                 for(int k=0;k<count-1;k++){                     if (temp==""){                         sb.append(str.charAt(start-1));                     }else {                         sb.append(temp);                     }                 }                 temp = "";                 i--;//若存在数字,此时i指向的字符并没有经过上述判断,所以需要减去1.             }else {                 sb.append(str.charAt(i));             }         }         return sb.toString();     }
点赞 评论

相关推荐

01-07 07:54
已编辑
门头沟学院 前端工程师
点赞 评论 收藏
分享
Cassifa:发的字比你都多的一律视为骗子或者想白嫖压榨实习生的
点赞 评论 收藏
分享
牛客网
牛客企业服务