1 回答

TA貢獻1831條經驗 獲得超10個贊
這是代碼?;旧显谒凶址麛低曛蟆T诖蛴≈?,我向后看以確保它不是重復的。這里有很大的優化空間,比如在計數之前進行檢查,或者可以只計算 i 之后的字符而不是計算所有字符。
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;
}
}
添加回答
舉報