美文网首页
658.Swap Without Extra Variable

658.Swap Without Extra Variable

作者: 博瑜 | 来源:发表于2017-07-20 21:18 被阅读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;
}
};

相关文章

网友评论

      本文标题:658.Swap Without Extra Variable

      本文链接:https://www.haomeiwen.com/subject/jrwjkxtx.html