java反向编码字符串
我想寻求帮助,了解如何反转我的代码,以便输入“A2B5C2”将给我输出“AABBBCC”,有什么建议吗
谢谢
public static void printRLE(String str) {
int n = str.length();
for (int i = 0; i < n; i++) {
// Count occurrences of current character
int count = 1;
while (i < n - 1 && str.charAt(i) == str.charAt(i + 1)) {
count++;
i++;
}
// Print character and its count
System.out.print(str.charAt(i));
System.out.print(count);
}
}
public static void main(String[] args) {
String str = "AABBBBBCC";
printRLE(str);
}
# 1 楼答案
给你:
# 2 楼答案
你可以这样做:
,输出
# 3 楼答案
为了得到这个例子,这个数字将超过9,我建议使用一个简单的正则表达式来匹配
letter+number
,然后只需重复字母所需的次数:使用
Streams
API可以将测试