Skip to content

Instantly share code, notes, and snippets.

@zhoutuo
Created February 11, 2013 05:48
Show Gist options
  • Select an option

  • Save zhoutuo/4752911 to your computer and use it in GitHub Desktop.

Select an option

Save zhoutuo/4752911 to your computer and use it in GitHub Desktop.
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321
class Solution {
public:
int lastDigit(int x) {
return x % 10;
}
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() functio
int result = 0;
while(x != 0) {
result += lastDigit(x);
x /= 10;
result *= 10;
}
result /= 10;
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment