题解
class Solution {
public void reverseString(char[] s) {
char temp;
for(int i=0,j=s.length-1; i<=j; i++,j--) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
Java知识点
交换两个数字的三种方法
1、temp
2、异或
public class Solution1229 {
public static void main(String[] args) {
int a = 6;
int b = 12;
a = a ^ b;
System.out.println(a);
b = a ^ b;
System.out.println(b);
a = a ^ b;
System.out.println(a);
}
}
这里6在底层是 0110,12在底层是1100,Java中的异或符号是 ^。
a^b =0110 ^1100 = 1010
二进制的1010是10。
3、
public void swap (int a, int b) {
a = a+b;
b = a-b;
a = a-b;
}