例如,如果我有一個輸入像:爪哇RLE升級算法
Heeeeeeeeeeeellooooo
輸出必須是:
H9e3e2l5o
這是我到目前爲止寫的代碼:
個public class RLE {
public static String encode(String s) {
if (s == "" || s == null) return "";
StringBuilder sb = new StringBuilder();
int count = 1;
char previous = s.charAt(0);
char current;
for (int i = 1; i < s.length(); i++) {
current = s.charAt(i);
if (current == previous) {
count++;
} else {
if (count == 1) {
sb.append(previous);
} else if (count > 1) {
sb.append(count).append(previous);
count = 1;
}
}
previous = current;
}
return sb.toString();
}
結果:
'Heeeeeeeeeeeellooooo' - > H12e2l
你的問題是工作? –
我知道你的代碼不能提供預期的輸出。但是我不明白 - 那個說你的Hello應該導致H9e3e的規則是什麼......(你的第一個例子是把12分成9 + 3 ......但是......爲什麼?爲什麼是9 + 3? – GhostCat