Skip to content

Instantly share code, notes, and snippets.

@gja
Created April 16, 2013 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gja/5394121 to your computer and use it in GitHub Desktop.
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.
// 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