行程减压 [英] Run-length decompression
本文介绍了行程减压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
CS学生在这里。我想编写一个程序,该程序将解压缩根据游程长度编码的修改形式编码的字符串(我已经为其编写了代码)。例如,如果字符串包含 bba10,它将解压缩为 bbaaaaaaaaaa。如何使程序识别字符串的一部分( 10)是整数?
CS student here. I want to write a program that will decompress a string that has been encoded according to a modified form of run-length encoding (which I've already written code for). For instance, if a string contains 'bba10' it would decompress to 'bbaaaaaaaaaa'. How do I get the program to recognize that part of the string ('10') is an integer?
感谢阅读!
推荐答案
一个简单的正则表达式即可。
A simple regex will do.
final Matcher m = Pattern.compile("(\\D)(\\d+)").matcher(input);
final StringBuffer b = new StringBuffer();
while (m.find())
m.appendReplacement(b, replicate(m.group(1), Integer.parseInt(m.group(2))));
m.appendTail(b);
其中复制
是
String replicate(String s, int count) {
final StringBuilder b = new StringBuilder(count);
for (int i = 0; i < count; i++) b.append(s);
return b.toString();
}
这篇关于行程减压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文