Swap Two Integer without Temporary Variable
How to swap two integer without temportary variable ?
可以用XOR:
public class SwapTwoIntegerWithoutVariable { public static void main(String[] args) { int a = Integer.MAX_VALUE; int b = Integer.MIN_VALUE; a = a^b; b = a^b; a = a^b; System.out.println("a "+a); System.out.println("b "+b); } }
Leave A Comment