1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
這是代碼?;旧显谒凶址麛?shù)完之后。在打印之前,我向后看以確保它不是重復(fù)的。這里有很大的優(yōu)化空間,比如在計(jì)數(shù)之前進(jìn)行檢查,或者可以只計(jì)算 i 之后的字符而不是計(jì)算所有字符。
public static void EvenPairs(String s) {
char[] chs = s.toCharArray();
int count = 0;
for (int i = 0; i < chs.length; i++) {
for (int j = 0; j < chs.length; j++) {
if (chs[i] == chs[j]) {
count++;
}
}
boolean shouldPrint = true;
for (int k = i - 1; k >= 0; k--) { //loop though each character before the current one to check if it was already printed.
if (chs[k] == chs[i]) { //if we it was already printed don't print.
shouldPrint = false;
break;
}
}
if (shouldPrint) {
if (count % 2 == 0)
System.out.println(s.charAt(i) + "- true");
else
System.out.println(s.charAt(i) + "- False");
}
count = 0;
}
}
添加回答
舉報(bào)