String input = "AAAB";
String output = "";
for (int index = 0; index < input.length(); index++) {
if (input.charAt(index % input.length()) != input
.charAt((index + 1) % input.length())) {
output += input.charAt(index);
}
}
System.out.println(output);
但是,如果我的輸入是「ABABAB」或只是「AAAA」,則不起作用。有任何想法嗎?刪除字符串中的重複項而不使用數組
我並不完全相信你的意圖是什麼,你可以添加輸入和匹配預期輸出的例子嗎? – bowmore
您需要定義:*不使用數組*。 'input.charAt(..)'正在使用一個數組,例如... – assylias