Document directory
- Exchange two variables without referencing the third variable.
Exchange two variables without referencing the third variable. A few days ago, I went to the interview and was asked this question. At that time, I couldn't answer it, and he didn't care. the second company was still asked this question, but still did not answer it. come back and ask me, I don't know how to solve it. I think there must be a good solution, but I just can't think of it. there were still many students who did not know about the party in Suzhou last night. After being explained by the singer, they could find a good solution.
As follows:
Void swap (int a, int B)
{
A = a ^ B;
B = B ^ a; // B ^ a is equal to B ^ a ^ B, that is, the value of B ^ a ^ B is a, and the following is the same
A = a ^ B;
}
In fact, it is very simple, just don't think of it. If you want to go to the interview, can it be useful?
The above method should be swap (ref int a, ref int B). To keep the original color, do not change it. This sentence is added on the 29th.