Created
February 11, 2013 05:48
-
-
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
This file contains hidden or 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
| 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