658.Swap Without Extra Variable

2017-07-20  本文已影响0人  博瑜

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;
}
};
上一篇 下一篇

猜你喜欢

热点阅读