Created
July 9, 2013 08:02
-
-
Save luoxiaoxun/5955548 to your computer and use it in GitHub Desktop.
Reverse digits of an integer. Example1: x = 123, return 321
Example2: x = -123, return -321 Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. Did you notice that the …
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
C++: | |
class Solution { | |
public: | |
int reverse(int x) { | |
// Start typing your C/C++ solution below | |
// DO NOT write int main() function | |
int res=0; | |
while(x!=0){ | |
res=res*10+x%10; | |
x=x/10; | |
} | |
return res; | |
} | |
}; | |
Java: | |
public class Solution { | |
public int reverse(int x) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int res=0; | |
while(x!=0){ | |
res=res*10+x%10; | |
x=x/10; | |
} | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment