两个变量间的就地替换(in-place)

两个变量之间交换值,不使用第三个变量,也称为原地算法(in-place algorithm) 。

--java--public class R {

 public static void main(String[] args) {
  int x = 1, y = 2;
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.println("x:" + x + ",y:" + y);

  // or

  x = x ^ y;
  y = x ^ y;
  x = x ^ y;

  System.out.println("x:" + x + ",y:" + y);

 }

}