Created
April 16, 2013 07:38
-
-
Save gja/5394121 to your computer and use it in GitHub Desktop.
Swap two numbers.
Thought process behind this: You aren't able to store data in any new variables, so the only option available to you is the stack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume a < b | |
void swap(int &a, int &b) | |
{ | |
if(a < b) { | |
a++; | |
swap(a, b); | |
b--; | |
} | |
} | |
// If you do not want to assume a < b, you can add the following snippet to the top of the function | |
// if(b < a) return swap(b, a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment