以下は、「その場で」文字列を逆にする、つまり Black Cat が Cat Black になるメソッドです。2 番目のスワップ セクションでは、従来のスワップ (コメント アウト) を使用するとすべてのテストに合格しますが、XOR スワップを使用すると 1 つのテストしか合格しません。
単に「交換」することはできませんか
for (int i = count; i <= (end + count) / 2; i++) {
char temp = arr[i];
arr[i] = arr[end - (i - count)];
arr[end - (i - count)] = temp;
}
に
for (int i = count; i <= (end + count) / 2; i++) {
arr[i] ^= arr[end - (i - count)];
arr[end - (i - count)] ^= arr[i];
arr[i] ^= arr[end - (i - count)];
}
方法
public class ReverseString {
public static char[] revString(String input) {
char[] arr = input.toCharArray();
int length = arr.length;
for (int i = 0; i < (length / 2); i++) {
arr[i] ^= arr[length - i - 1];
arr[length - i - 1] ^= arr[i];
arr[i] ^= arr[length - i - 1];
}
int end;
int charCount;
int count = 0;
while (count < length) {
if (arr[count] != ' ') {
charCount = 0;
while (count + charCount < length && arr[count + charCount] != ' ') {
charCount++;
}
end = count + charCount - 1;
// for (int i = count; i <= (end + count) / 2; i++) {
// char temp = arr[i];
// arr[i] = arr[end - (i - count)];
// arr[end - (i - count)] = temp;
// }
for (int i = count; i <= (end + count) / 2; i++) {
arr[i] ^= arr[end - (i - count)];
arr[end - (i - count)] ^= arr[i];
arr[i] ^= arr[end - (i - count)];
}
count += charCount;
} else {
count++;
}
}
return arr;
}
}
テスト
@RunWith(JUnitParamsRunner.class)
public class ReverseStringTest {
@Test
@Parameters(method = "getStrings")
public void testRevString(String testValue, char[] expectedValue) {
assertThat(ReverseString.revString(testValue), equalTo(expectedValue));
}
private static final Object[] getStrings() {
return new Object[] {
new Object[] {"Black Cat", "Cat Black".toCharArray()},
new Object[] {"left to", "to left".toCharArray()}
};
}
}
失敗時の出力
java.lang.AssertionError:
Expected: ["C", "a", "t", " ", "B", "l", "a", "c", "k"]
but: was ["C", "