658.Swap Without Extra Variable (Only C++)

Given two variables, x and y, swap two variables without using a third variable.

class Solution {
    public:
    /**
 * @param x an integer
 * @param y an integer
 * @return nothing
 */
void swap(int &x, int &y) {
    // Write your code here
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
}
};

你可能感兴趣的:(658.Swap Without Extra Variable (Only C++))